Reece Hodge put the icing on the cake on the last play of the match, burning two Italian defenders with sheer pace in a 50m burst to score a try after some clever work from Foley created space for his outside backs.
The Wallabies endured an indifferent opening to the match. Some horrendous handling in the first few minutes gave Italy every opportunity to score, and only a controversial call by the TMO prevented the Azzurri from registering the first try.
The visitors dominated possession and field position for the entirety of the first stanza but when Australia found its groove in attack it was hard to stop, piling on three tries in the first 40 minutes. Israel Folau made history by scoring two of them, becoming the first Wallaby to score two or more tries in three consecutive tests as he continued the try-scoring feast he'd been enjoying against Scotland and Fiji in the past two weeks.
Italy - who had already potted two penalty goals - hit back with a try on the break five minutes before half-time to reduce the deficit to eight. A clever inside ball put an Italian runner through a hole on halfway and an offload found centre Michele Campagnaro, who had too much pace for the Aussies in a 40m sprint.
The second half resumed with Australia up 21-13, and their attack was slick from the resumption as some off-the-cuff play saw the Wallabies score their fourth try.
From a couple of metres out Foley scooped a pass through his legs to Dane Haylett-Petty, who in turn found Karmichael Hunt. The second-five threw a long spiral pass out to Sefanaia Naivalu who scored his second try of the match before going off with a knee injury shortly after.
The hosts bombed a couple of tries midway through the second half as loose passes went over the sideline, and their poor execution came back to haunt them as Italy scored two tries in quick succession to make it a one-point ball game.
Haylett-Petty couldn't hold on to a cross-field bomb on the Australian goal-line and Italian fullback Edoardo Padovani scooped up the loose ball to score before Tommaso Benvenuti intercepted a Rory Arnold pass deep inside Australian territory to rack up another five-pointer.
The Wallabies would endure even more nervous moments as Italy threatened to take the lead, but they held on for an unconvincing win.
Australia 40 (I. Folau 2, S. Naivalu 2, B. Foley, R. Hodge tries; Foley 5 cons)
Italy 27 (T. Benvenuti, M. Campagnaro, E. Padovani tries; T. Allan 3 cons, 2 pens).