Another comeback victory led to history for the Samurai Blue.
Japan rallied for two goals in a three-minute span against Spain in a 2-1 victory as the nation won its group for the first time in World Cup history. Japan, which also erased a one-goal deficit in a 2-1 win over Germany, topped Group E with six points.
Japan advances to the round of 16 for the second straight World Cup and fourth time in the last six tournaments. They will be looking for their first victory in the knockout stage.
Spain, despite the loss, also advances to the round of 16 after Germany defeated Costa Rica 4-2. Dani Olmo nearly scored the equalizer for Spain in the 89th minute, a goal that likely would have knocked Japan out of the tournament and given Spain the top spot in the group. But his shot off a give-and-go in the box was stopped by a diving Shuichi Gonda.
Spain and Germany finished tied with four points, with Spain winning the goal differential tiebreaker to advance.
Spain took an early 1-0 lead after Álvaro Morata scored on a header in the 11th minute for his third goal in three games.
Get Tri-state area news delivered to your inbox. Sign up for NBC New York's News Headlines newsletter.
Local
Spain dominated the first half, controlling possession for 78 percent of the time and completing 530 passes. But Japan struck early in the second half, scoring two goals in a three-minute span.
Ritsu Doan fired a left-footed shot past Spain keeper Unai Simón for the equalizer in the 48th minute.
Kaoru Mitoma later slid to get a touch on the ball just before it crossed the endline, sending it in front of the empty net, where Ao Tanaka knocked it in to give Japan a 2-1 lead in the 51st minute.
A back-and-forth affair between Germany and Costa Rica kept the pressure on for Spain and Japan. But both teams ultimately advanced to the round of 16, which begins on Saturday.