Goals from Ritsu Doan and Ao Tanaka helped Japan reach the World Cup last 16 as Group E winners with a remarkable 2-1 victory over Spain, with La Roja joining them as Germany were dumped out.
With both sides knowing a win would see them advance, Spain wasted little time in hitting the front as Alvaro Morata headed beyond Shuichi Gonda for his third goal of the tournament early on.
However, Spain failed to capitalise on their huge dominance of possession as Japan turned the game on its head with a quickfire second-half double, Doan firing home from range before Tanaka bundled in a second.
Spain were briefly on their way out of the World Cup when Costa Rica took the lead against Germany in the group's other fixture.
But Germany's late comeback in a 4-2 win ensured La Roja survived an incredible finale to go through as runners-up.