Richarlison continued his fine form at the Olympics with a double as Brazil eased into the quarterfinals after a 3-1 win over Saudi Arabia on Wednesday, as Germany exited the men's tournament and hosts Japan progressed with a thrashing of France.
Reigning champions Brazil, who won gold for the first time on home soil in Rio de Janeiro five years ago, finished top of Group D and will face Egypt in the last eight on Saturday.
The Germans, runners-up to Brazil in 2016, bowed out after a 1-1 draw with the Ivory Coast.
Germany had to win to progress, but Benjamin Henrichs' own goal midway through the second half proved costly.
Eduard Lowen's equaliser shortly afterwards set up a tense finale, but the Ivorians held on to seal second spot in the group and a quarter-final against Spain.
The Spaniards are one of the gold medal favourites after naming a strong squad featuring six members of the team that reached the Euro 2020 semi-finals.
But they scored only twice in the group stage, with a 1-1 draw against Argentina on Wednesday enough to edge them through.
Egypt set up a dream clash with Brazil by seeing off Australia 2-0 with goals from Ahmed Yasser Rayan and Ammar Hamdy.
France crashed out as their defence proved fragile again in a 4-0 loss to Japan.
South Africa's slim hopes of reaching the quarters were ended by a 3-0 loss to Mexico in a game which ended with both teams a man down. The Mexicans will take on South Korea on Saturday, in the same half of the draw as Brazil and Egypt.
South Korea finished top of Group B after a 6-0 thrashing of 10-man Honduras and were joined in the knock-out stage by the Kiwis, who played out a goalless draw with Romania to finish above their opponents on goal difference.