Germany crashed out of the World Cup in the group stage despite a 4-2 win over Costa Rica on Thursday.
Japan and Spain qualified for the last 16 from Group E after Japan’s 2-1 victory, which made them group winners, although there was controversy about their winner over whether the ball was out of play.
Japan next play 2018 runners-up Croatia in the last 16 on Monday while Spain face Morocco on Tuesday.
On a night of high drama in Qatar, Germany and Spain both took early leads through Serge Gnabry and Alvaro Morata in results which would have put Germany through.
But it all changed in the second half with Japan scoring two quick goals against the Spanish, substitute Ritsu Doan levelling before the debatable second from Ao Tanaka.
The officials originally ruled it out but the video assistant decided not all of the ball had gone out of play despite some replays suggesting it had.
If Japan had drawn 1-1 and not won, Germany would have gone through on goal difference.
Around the same time, Costa Rica also equalized through Yeltsin Tejeda against the Germans, who then hit the post three times before the Central Americans went ahead in a scramble that was eventually given as a Manuel Neuer own goal.
That would have put Costa Rica through at the expense of Spain, but substitute Kai Havertz scored twice and Niclas Füllkrug got another.
It is the second World Cup in a row where four-times champions Germany have been eliminated in the group stage.
They began their Qatar campaign with a shock 2-1 defeat by Japan but fought well in the 1-1 draw with Spain.
“We didn’t crash out today, we crashed out in those 20 minutes against Japan,” Germany coach Hansi Flick told ARD.
“We lacked efficiency and this is why we are out.”
Flick had said before the game that he was planning to stay in the job whatever happened on Thursday. He has a contract until 2024, when Germany hosts the European Championship.
Storied Germany are the biggest casualties of the competition so far, with second-ranked Belgium also going out earlier on Thursday.
“For me personally this is an absolute catastrophe,” Germany forward Thomas Müller, 33, said, suggesting he will now retire from international football.
“If this was my last game, I want to say a few words to our fans who have supported me for years. I have always tried to leave my heart on the field.”
The game was also historic as it was the first men’s World Cup game to be refereed by a female – Frenchwoman Stéphanie Frappart.
Coach Flick took over from 2014 World Cup-winning coach Joachim Löw last year after a disappointing last-16 exit at the Euros.
Flick made a great start, winning his first eight games in charge but results in the Nations League then dropped off.
The damage was then done in the first two games in Qatar as Germany were left without qualification being in their own hands.
Spain, the 2010 champions, also played with fire but managed to squeeze through.
Luis Enrique made five changes, with striker Morata coming into the starting line-up after goals off the bench in the previous two group games.
The game plan was going well until a sudden Japanese burst in the second half, exactly like in the Japanese win over Germany.
“Everyone talks about a miracle, but I don’t see it that way,” said Japan’s Tanaka. “We became more and more aggressive. I felt Spain didn’t attack as much after Germany made it 3-2.
“We’ve never been to the quarter-finals, we want to do that now,” added Tanaka, who plays in Germany for Fortuna Dusseldorf but had little sympathy for the Germans. “That is football.”
Ultimately Spain’s 7-0 thrashing of Costa Rica in the first game put them through as they edged Germany on goal difference.
Spain’s César Azpilicueta said: “We always give everything. We always go out to win. Sometimes we get punished. We are disappointed with the defeat.”