Neymar scored twice as Brazil thrashed Cameroon 4-1 to reach the World Cup knockout rounds just ahead of group rivals Mexico on Monday.
A thrilling finale to Group A saw the hosts surge past Cameroon in Brasilia while Mexico downed Croatia 3-1 in Recife. The Netherlands beat Chile 2-0 to take top spot in Group B.
Brazil finished top of their group on seven points, leading Mexico, also with seven, on goal difference.
Brazil will play Group B runners-up Chile in Belo Horizonte on Saturday while Mexico will face the Netherlands in Fortaleza on Sunday.
Brazil coach Luiz Felipe Scolari said he was pleased his side had shown signs of improvement — but admitted he would have preferred to have avoided Chile.
“I feel good now that we have qualified and I am thinking about my team. I know that they are going to keep improving,” said Scolari.
“I have played against Chile two times before and I know how difficult a side they are. Some people think we will beat Chile easily but they are a quality side, well organised.
“If I could have chosen our opponents I would not have chosen them.”
Star striker Neymar opened the scoring in the 17th minute — the 100th goal of the tournament in Brazil’s 100th World Cup match — to set the hosts on their way.
A close-range effort by Cameroon’s Joel Matip jolted Brazil on 26 minutes before Neymar settled nerves again with his second of the match nine minutes later.
Brazil stretched their lead through Fred after the interval and looked to be cruising to first place.
But a flurry of three Mexico goals in 10 minutes from Rafael Marquez, Andres Guardado and Javier Hernandez against Croatia changed the complexion of the qualifying picture.
With Mexico rampant and leading 3-0, one more goal could have seen them pip Brazil to first place in Group A.
Panic was short-lived however, with substitute Fernandinho stroking home Brazil’s fourth to restore the hosts goal difference advantage.
A late consolation goal by Croatia’s Ivan Perisic snuffed out any chance of a remarkable turnaround by Mexico.
In Monday’s earlier games, the Netherlands ensured top spot in Group B — and a date with Mexico — by beating Chile 2-0 in Sao Paulo.
Super-subs Leroy Fer and Memphis Depay grabbed the late goals to settle a hard-fought clash.
The victory gave the 2010 runners-up a 100 percent record after earlier wins against Spain and Australia.
The Netherlands, missing suspended captain Robin van Persie, finally broke through Chile’s defence on 77 minutes.
Daryl Janmaat whipped in a cross and Fer rose high to thunder home a header.
In the dying minutes, Arjen Robben burst clear down the left flank and crossed for Depay to stab home from close range.
“We have to take time to enjoy this fantastic result but it shouldn’t end here,” said Robben.
“We have to change our mindset and look ahead because we really want to move forward.”
Spain defeated Australia 3-0 in Group B’s dead rubber, with David Villa, Fernando Torres and Juan Mata scoring.
Villa, 32, took his Spanish record goals tally to 59 in his 97th appearance with a clever backheeled finish on 36 minutes, in his last international appearance.
But Villa’s goalscoring finale was scant consolation for Spain after a disastrous World Cup campaign which saw the 2010 champions knocked out after their first two matches.
Villa, who will concentrate on a new club football career in Australia and the United States, was substituted early in the second half, shedding tears as he left the field to applause.
“I have always had a lot of love for the national team and it has always been what I dreamed about,” Villa said afterwards.
“I would play until I am 55 but that is impossible and I have to be realistic.”
Tuesday’s games see Italy and Uruguay battling to avoid elimination in Group D where a win for either side will send them through to the last 16,
Costa Rica meanwhile will attempt to win the group, needing just to avoid defeat against England in Belo Horizonte.