Neymar proved the inspiration once again as Brazil gave themselves the best chance of avoiding a semi-final with Spain by topping Group A with a dramatic victory over Italy in Salvador.
The 21-year-old lit up the Arena Fonte Nova with a spectacular second-half free-kick while striker Fred scored twice to set the Selecao on their way to a third straight win in the Confederations Cup.
Substitute Dante had prodded in to give Brazil the lead on the stroke of half-time, only for Emanuele Giaccherini to bring Italy level with a fierce shot early in the second half.
Neymar then produced his moment of magic, before Fred appeared to put the game beyond Italy with 23 minutes remaining.
Italy, however, came roaring back with a controversial and scrappy goal from Giorgio Chiellini. The Azzurri had the better of the final quarter of the match but Brazil made certain late on when Fred pounced as Gianluigi Buffon failed to hold Marcelo's shot to send the stadium into raptures.
The result means Brazil are likely to meet Uruguay in a semi-final in Belo Horizonte on Wednesday. Italy face the prospect of a last four meeting with world champions Spain in Fortaleza, 24 hours later.
Much too had been made of the battle between two of the world's most gifted young players in Neymar and Mario Balotelli. Both were forced to ride some fierce, physical challenges throughout the game but underlined their immense talent with outstanding displays at either end.
Brazil looked a side bristling with menace in the early exchanges. Hulk's rasping low shot forced Italy captain Buffon into a smart save.
The importance of winning the game and avoiding Spain in the semi-finals was evident from Brazil's formidable starting line-up. The dead leg that forced David Luiz off the field will be cause for concern, although it appeared more of a precaution for the Chelsea defender. It mattered not. His replacement Dante, from Bayern Munich, made an almost immediate impact just before half-time.
Neymar delivered an inviting free-kick which Fred headed goalwards and forced an instinctive low save from Buffon. The ball rolled clear, however, and Dante was on hand to prod it back into the net at the near post.
Italy would not lie down, though. Their response stunned the stadium into silence as Giaccherini raced on to Balotelli's delightful flick before firing an unerring shot beyond Julio Cesar.
Neymar then struck after being dumped on his backside on the edge of the penalty area. The stadium held its breath as the Barcelona new boy lined his free-kick up, took two steps and curled the ball over the wall and beyond Buffon's left hand. The Arena Fonte Nova erupted and fireworks exploded in the skies around the stadium.
Balotelli's then thumped a dipping free-kick at goal from all of 40 yards that required Cesar to claw it away from under his bar but it was Brazil who scored next to extend their lead.
Fred latched onto a long ball forward, held off Chiellini and steered his shot high into the net. And yet with the game all but over, Brazil allowed Italy back in.
The hosts failed to clear a corner effectively as Marcelo grappled with Balotelli, the ball ran to Chiellini and he rolled a scrappy shot into the bottom corner to give the Azzurri hope. Brazil fiercely contested the awarding of the goal and claimed the referee had blown his whistle for a penalty, a moment before it was scored.
Joy had given way to tension by now as Italy attacked. And yet Brazil would have the final word as Fred was on hand to thump in after Buffon saved Marcelo's shot to keep the hosts on course for a possible final with Spain in Rio.