Substitute defender Dante opened the scoring from close range in first-half injury time, but Italy equalized through Emanuele Giaccherini's swift breakaway in the 51st. Dante looked just offside for his goal.
Neymar scored in the 55th with a well-placed free kick shot into the top corner of the net as Italy goalkeeper Gianluigi Buffon could only watch, and Fred added to the lead from inside the area in the 66th before Italy pulled closer again with a Giorgio Chiellini goal in the 71st.
Brazil was furious with Uzbek referee Ravshan Irmatov, who appeared to blow for an Italy penalty but changed his mind and gave a goal when it went in the net a second or so later.
Italy almost equalized when an 80th-minute header by Christian Maggio exploded off the crossbar, and Mario Balotelli just missed with a right-footed shot a few minutes later, but Fred sealed the victory from a Buffon parry in the 89th, another goal that looked offside.
Brazil ended with nine points, three more than the Italians. Mexico and Japan had already been eliminated. Brazil needed at least a draw to win the group and likely avoid a matchup with World Cup champion Spain, which leads Group B.
"Brazil is ready for the semifinal," Brazil coach Luiz Felipe Scolari said. "We've made it with a team with good technical and tactical quality and with a lot of confidence."
Italy coach Cesare Prandelli was pleased with some of his team's performance. "In the second half we played great. I'm very pleased with what I saw, the way we reacted and had desire to attack deep. Of course, against squads like these you always risk something and if you make a mistake you let in a goal.
"Matches like these make you grow and if we face Spain we'll try to play our game," he said.
The result keeps Italy winless against Brazil in 30 years, since the second round of the 1982 World Cup in Spain. In six matches since, Brazil has won four and there were two draws.
Brazil had also beaten Italy 3-0 the group stage of the 2009 Confederations Cup, when it eventually won its second-straight title — and third overall — in the warm-up competition. Brazil hasn't won any significant title since then.
The teams had met only four times since the 1994 World Cup final in the United States, with two victories by Brazil and two draws, including a 2-2 result earlier this year. It was the first time the teams played in Brazil.
Brazil took control in the first few minutes but it didn't take long before Italy levelled the match at the Arena Fonte Nova in Salvador.
"We feared Brazil a little in the first half and you can't do that against a team like Brazil," Prandelli said. "In the second half we played in a different way. We showed personality and we are satisfied with how the team reacted. I'm happy with the way the team fought back and tried to make it difficult for Brazil."
Dante, who replaced injured starting defender David Luiz in the 34th, put Brazil ahead with a left-footed shot off a rebound from a difficult save by Buffon following a header by striker Fred. Buffon dived to his left to make the stop, but Dante was in position to sidefoot the ball into the net. Dante appeared to be narrowly offside after Fred's header from the free kick taken by Neymar.
Italy equalized with Giaccherini's powerful shot into the far corner after receiving an acrobatic pass by Balotelli as he was hit by Dante outside the area. Giaccherini had come on in the 26th to replace injured midfielder Riccardo Montolivo.
Neymar found the net from the free kick after he was fouled just outside the box following a nice run past Italian defenders. Buffon began going to his right and couldn't get back after Neymar fired the shot away from the wall instead of going over it.
"Neymar can be decisive and that's what happened with the free kick," Scolari said. "He had many good quality opportunities but the key thing was when he realized Buffon was taking a step to the side. Only those geniuses who have a notion of what is happening in the match can do that."
Neymar was replaced in the 69th to a standing ovation by the nearly 50,000 fans packing the Arena Fonte Nova. The recently signed Barcelona player was chosen the man of the match for the third match in a row.
Fred took advantage of a long pass by Marcelo and scored his first goal with a firm left-footed shot into the top of the net after battling with Italian defender Giorgio Chiellini inside the area.
Chiellini pulled Italy closer with a shot from near the penalty spot after a corner kick taken by Antonio Candreva, and after the missed chances by Maggio and Balotelli, Fred found the net again off the rebound of Buffon's save after a shot by Marcelo in a breakaway.
Italy also lost Ignazio Abate because of an injury just 30 minutes into the match.
The teams started without some of their most important players. Italy couldn't count on midfielder Andrea Pirlo because of a strained right leg muscle, while Brazil was without defensive midfielder Paulinho because of a left ankle sprain. The Italians also missed Daniele De Rossi, who was suspended with two yellow cards.
Follow Tales Azzoni at http://twitter.com/tazzoni