Japan came from behind on Thursday night to beat Spain 2-1 and qualify for the last-16 at the World Cup in Qatar as winners of Group E as Germany crashed out of the tournament despite beating Costa Rica 4-2.
The Japanese went into the game at the Khalifa International Stadium as underdogs following a lacklustre 1-0 loss to Costa Rica during the second round of games.
That defeat, coupled with a 1-1 draw between Germany and Spain, left the group wide open for an array of permutations.
They came into play during a helter-skelter second-half in which Costa Rica came from behind to lead 2-1 Germany - and thereby eliminate Spain.
But the Germans soon restored parity thanks to a brace from Kai Havertz before adding a fourth through Niclas Fullkrug.
That two-goal margin meant that a Spanish equaliser against Japan would furnish Germany with a place in the last-16 with Spain.
The favour never arrived. Ritsu Doan and Ao Tanaka remained the Japanese heroes and Spain will take on the Group F winners Morocco on Tuesday.
The Germans, who lost to Japan in their opening game, were left to rue that setback as they were eliminated from back-to-back tournaments for the first time in the 92-year history of the competition
Japan will play Croatia, who advanced as runners-up from Group F following a 0-0 draw with Belgium.