The striker put the hosts 3-1 up and then struck again in the dying seconds to secure a win that should see Felipe Scolari's men avoid Spain in the semifinalsBrazil sealed top spot in Confederations Cup Group A with an action-packed 4-2 victory over old rival Italy in Salvador on Saturday night.
The Selecao dominated the opening 45 minutes but only made the breakthrough in injury time at the end of the half, with Dante firing home. Italy drew level in emphatic fashion through Emanuele Giaccherini just after the interval only to see Brazil quickly reassert its supremacy with equally fine strikes from Neymar and Fred.
The Azzurri, though, should have snatched a draw, with an unmarked Christian Maggio heading against the bar shortly after Giorgio Chiellini had dragged Cesare Prandelli's men back into the game, and they were punished for their profligacy by Fred, who netted his second of the game in the dying minutes.
Italy had initially picked up where it had left off against Japan: in complete disarray defensively. Indeed, Brazil twice went close to opening the scoring inside the opening three minutes, with Chiellini making a desperate last-ditch challenge to stop Oscar, before Gianluigi Buffon had to get down well to keep out a low, angled strike from Hulk.
Italy did its utmost to weather the early storm and even carved out an opening on 18 minutes when Claudio Marchisio drove forward down the left wing before pulling the ball back for Mario Balotelli, who badly scuffed his shot, resulting in the easiest of saves for Julio Cesar.
However, Italy was still being overrun in midfield and Cesare Prandelli unsurprisingly elected to make a first-half substitution for the second game in succession, sending Emanuele Giaccherini on in place of Riccardo Montolivo, with the dynamic Marchisio dropping back into midfield alongside Alberto Aquilani.
Sadly, the Azzurri were forced into a second change before the half-hour mark, with Ignazio Abate suffering a shoulder injury after being cynically taken out by Neymar, who, just moments beforehand, had been felled by the AC Milan fullback.
Even the crowd began to get tetchy the longer the half wore but the tension was broken just before the break, with Dante, on as a replacement for the injured David Luiz, exhibiting impressive composure in coolly slotting home the rebound, after Buffon had pulled off a terrific save to keep out a close-range header from Fred.
The second half looked set to continued in much the same vein as the first, with Brazil bossing possession and territory, and the hosts could even have doubled their advantage within a minute of the restart, but Oscar shot too tamely at Buffon, after the ball had dropped for the Chelsea ace on the edge of the area.
However, Italy then leveled out of nowhere. There appeared to be no danger when Buffon punted the ball long, but Antonio Candreva headed it on before Balotelli acrobatically flicked into space for Giaccherini. The much-maligned utility man appeared to hesitate for a second but he then unleashed a blistering drive from just inside the area that gave Julio Cesar no chance.
Order was restored almost immediately, though, with Neymar deceiving Buffon with a terrific free kick, after the Brazilian No.10 had himself been taken down on the edge of the Azzurri box.
Balotelli almost replied in kind on the hour, but his long-range set piece was fumbled away to safety by Julio Cesar. As it was, it was Brazil which struck next in an increasingly chaotic encounter, with Fred finishing in style, after his run in between Chiellini and Leonardo Bonucci was expertly picked out by Marcelo.
Italy was not done yet, though, and it set up a frantic finale when the ball dropped for Alberto Aquilani in the six-yard box, allowing him to tee up Chiellini, who scuffed the ball into the bottom left corner of the Selecao goal.
Maggio then crashed a header against the bar as Italy finished the stronger but it was caught cold on the break late on, with Fred on hand to convert after Buffon had been unable to hold onto a shot from Marcelo, meaning the Azzurri will most likely face Spain in the semis.