Japan overcame a stunned Spain side and advanced to the knockout stage of the World Cup.
This was the most dramatic night of the tournament so far, with Spain needing the result of Germany's game with Costa Rica to progress.
With Spain needing only a draw to qualify, they were not expected to need any help from anyone else.
La Roja were cruising at the break after Alvaro Morata headed home Cesar Azpilicueta's cross, but Japan quickly turned the game on its head.
Within six minutes of the restart, the two replacements for the Samurai Blue had made a big impact.
Doan scored with a fierce shot from the edge of the area and only 142 seconds later, Mitomo retrieved Doan's cross and found Ao Tanaka to score.
The goal was initially ruled out by the assistant referee, who thought Doan's cross had gone out of play, but the Video Assistant Referee ruled that the goal had actually been scored.
Things got worse for Spain when Costa Rica took a surprise lead over Germany at that time.
The updated group table was shown on the big screens at the stadium.
Germany's late rally sent La Roja through as group runners-up, despite the fact that they were in big trouble.
Japan will face Croatia in the last 16 while Spain will face Morocco.
The setting for Japan's victory over Germany in the opening game of this tournament was this stadium.
The high-pressing style that undid Germany had unsettled Spain at times in the first half, but there were still few signs of what would happen in the second half.
Morata equaled Telmo Zarra's record for scoring on his first three World Cup appearances for Spain when he netted his third goal of the tournament.
From the start of the second half, Japan refused to let Spain settle on the ball as they attempted to build from the back.
Doan started their revival, collecting the ball on the edge of the area after another loose pass forward by Simon, and firing in a thunderous shot that Simon was unable to keep out.
After a lengthy VAR check, more excitement would follow thanks to Tanaka.
Spain had virtually the entire of the second half to stage a recovery, but they lacked either energy or ideas, and rarely threatened the Japan goal.
The news of Germany's comeback allowed the Spain fans to relax, with the goals that La Roja scored in their opening win over Costa Rica proving decisive in sending them through.
Get the latest results and goal notifications for any team at the Fifa World Cup by downloading the BBC Sport app: Apple - Android - Amazon
You can get a daily dose of the World Cup on the radio.