Brazil finally found its scoring touch in the men's soccer tournament at the Olympics.
The host country advanced to the quarterfinals by beating Denmark 4-0 Wednesday, avoiding another embarrassing elimination in front of the home fans.
Gabigol scored twice and Gabriel Jesus and Luan added the others at the Arena Fonte Nova as Brazil continues its quest to win the Olympic gold medal in soccer for the first time.
"The team played a great match," Brazil coach Rogerio Micale said. "We found our game and this time the ball went in. That gave us a lot of tranquility during the match."
The Brazilians will next play Colombia on Saturday in Sao Paulo.
Brazil had been held to scoreless draws in its first two group games and needed a victory to guarantee a spot in the knockout round. It ended up first in Group A with five points, one more than Denmark, which also advanced and will play Nigeria in Salvador. South Africa and Iraq were eliminated after a 1-1 draw in Sao Paulo.
The convincing victory also helped Brazil regain the support of the local fans, who had been disappointed by lackluster performances and had loudly jeered the team in the games against South Africa and Iraq.
The crowd of more than 40,000 fans was behind the team from the start in Salvador and chanted "The champion is back" during parts of the game.
"To have the crowd on our side is extremely important," Micale said. "Hopefully it will be like that again in Sao Paulo."
Led by Neymar, Brazil arrived as the main favorite to win the Olympic tournament and finally breakthrough with the gold medal, which is the only trophy the five-time world champions haven't won in soccer.
Needing the victory, Brazil attacked from the start and took the lead after Gabigol's close-range shot after a low cross by Douglas Santos in the 26th minute. Gabriel Jesus added to the lead from near the goal line in the 40th after a pass by Luan, who scored the third from near the penalty spot in a play started by Neymar. Gabigol's second goal came with a shot off a rebound in the 80th.
Neymar, who had been heckled by some fans after struggling in the first two games, played better and was much more involved in the game. Gabigol and Gabriel Jesus, the 19-year-old forwards touted as some of Latin America's most promising players, also improved and finally found the net.
Brazil was in danger of another humiliating elimination just two years after the 7-1 loss to Germany at its home World Cup. The senior team also disappointed at the Copa America two months ago, failing to advance from the group stage of the continental competition.
Earlier Wednesday, two-time Olympic champion Argentina and defending champion Mexico were eliminated from medal contention after failing to advance from the group stage.
Playing without any of its biggest stars, Argentina needed a win against Honduras but was held to a 1-1 draw at the Mane Garrincha Stadium in Brasilia. It was another blow to Argentine soccer following the senior team's loss in the Copa America final in June and the ongoing crisis enveloping the sport at home.
Mexico, which brought a stronger team and was among the title favorites, lost to South Korea 1-0 in Brasilia to end its chances of repeating its triumph from four years ago, when it upset Brazil in the final in London.
South Korea will next play Honduras in Belo Horizonte.
Germany will play Portugal in Brasilia after advancing to the quarterfinals with a 10-0 rout of Fiji at the Mineirao Stadium in Belo Horizonte, the same place where the country's senior team embarrassed Brazil 7-1 in the semifinals of the 2014 World Cup. Fiji had already lost 5-1 to Mexico and 8-0 to South Korea.
All quarterfinal matches will be on Saturday.