Spain led through Alvaro Morata's early goal, but Japan, who couldn't manage a shot on target in the first half, turned the game around shortly after the break with two goals during a frantic three-minute spell thanks to halftime substitute Ritsu Doan and Ao Tanaka.
Tanaka's goal was initially ruled out after the ball was judged to have run out of play before Kaoru Mitoma's cut-back, but VAR overturned the decision and gave Japan the three points they needed to be sure of a place in the knockout rounds.
1. Japan's special run continues
Japan gave notice four years ago that they're not phased by facing heavyweight opposition at the World Cup when they almost dumped Belgium out in the round of 16, but what they have already done in Qatar is something special.
Despite being drawn in a tough group, there was a quiet confidence at home about what Hajime Moriyasu's team might be able to achieve. Very few would have expected to top Group E by beating both Germany and Spain. Even more remarkable is that Japan were a goal down in both games and battled back -- a warning to any team they face in the knockout rounds that they are never out of a contest.
Since qualifying for their first World Cup in 1998, Japan have never reached the quarterfinals, but they have already shown here that anything is possible, starting with a knockout round matchup against Croatia on Monday.
It also becomes full circle for Moriyasu, who played in a World Cup qualifier against Iraq in Qatar in 1993 which became known as "the tragedy of Doha" after Japan conceded a stoppage time equaliser to deny them an appearance at the 1994 tournament.
This result will surely soothe that painful memory for Moriyasu as his team continue an improbable run in Qatar.
2. Spain's struggles a blessing?
After demolishing Costa Rica 7-0 in the group stage opener, looking comfortable for large spells against Germany and then taking an early lead against Japan, it's almost unbelievable that for a period in the second half here Spain were going out.
Japan's rally with two goals in three minutes early in the second half -- combined with Costa Rica taking an unlikely lead against Germany -- meant Spain dropped down to third in Group E and facing an early exit.
Spain manager Luis Enrique has Kai Havertz to thank for turning the game against Costa Rica in Germany's favour, but it will be a worry that when La Roja were desperate for a goal, they looked toothless.
The bonus for Spain is that finishing second in their group means they avoid the toughest side of the draw, which already includes Argentina and likely Brazil. Winning the group would have likely meant a quarterfinal matchup with Brazil, but now Spain's route includes Morocco in the second round, possibly Portugal in the quarterfinals and perhaps France or England in the semifinals.
However, none of that will matter if Spain keep playing like they did in the second half -- mistakes in defence and limp in attack -- because if they do that, they won't get very far at all.
ESPN FC's Gab Marcotti defends Morocco after Spain finish 2nd in their group and face them in the round of 16.
3. Morata makes case but attack depth an issue
Alvaro Morata has handed his first start of the World Cup and repaid Luis Enrique with a trademark header from Cesar Azpilicueta's pinpoint cross, but what followed was a lot of possession and not much else.
In the first half, both Rodri (115) and Pau Torres (109) completed more passes than Japan did as an entire team (89) but as is sometimes the case with Spain, it didn't really mean anything, and for spells of the game it almost looked like they wanted to finish second in the group.
Morata now has three goals in Qatar -- tying him with Kylian Mbappe, Marcus Rashford, Cody Gakpo and Enner Valencia in the Golden Boot race -- and has made a strong case to start against Morocco. However, picking him from the start would leave Luis Enrique without a direct option off the bench if things start to go wrong. As Spain chased a goal against Japan, Marco Asensio, Ferran Torres and Ansu Fati all came on, but they struggled to make a significant impact.
In the end Spain were fortunate that Costa Rica couldn't hold on against Germany, but they rarely troubled Japan goalkeeper Shuichi Gonda in the second half.
Best and worst performers
BEST: Ritsu Doan, Japan.
Changed the game when he came on at halftime, first with a spectacular goal and then with the cross which led to Japan's game-winner.
WORST: Unai Simon, Spain.
Gave the ball away for Japan's first goal trying to play out from the back.
Highlights and notable moments
If this ball is ruled out Germany would be moving on to the Round of 16 😱 pic.twitter.com/PcceKn0AIW— ESPN FC (@ESPNFC) December 1, 2022
Check out ESPN's breakdown on why VAR awarded Japan the decisive goal after the ball appeared to go out.
After the match: What the players and managers said
Japan manager Hajime Moriyasu, on the win: "We played against Spain, one of the best teams in the world and we knew before the game that this was going to be very tough and difficult, and indeed it was," said Moriyasu whose side lost to Costa Rica in their second match in Qatar.
"The players conceded one goal, but they persisted and in very difficult circumstances, they did very well."
Spain manager Luis Enrique to TVE, on the result: "This is football, I have a lot of experience in football, I've seen everything, all the good and all the bad. Relax, try to make a difference with substitutions, we put on more attackers but it's hard to find spaces against an opponent that defends like Japan.
"We'll do our grieving tonight. There's nothing to celebrate, we go through in second, but this has to be a warning, to realise that this will happen. If we're lucky enough to go ahead against Morocco, this will happen again. We need to be a bit more convincing in those moments when we're under pressure."
Key stats (provided by ESPN Stats & Information research)
- Japan's 18% possession against Spain is the lowest by any team at a World Cup game since 1966.
- Japan are the second team to defeat both Spain and Germany at a single World Cup, joining Austria's 1978 squad.
Spain: Taking on Group D winners Morocco in the round of 16 on Monday. It is the second straight World Cup that the two geographical neighbors will face each other, with La Roja and the Atlas Lions playing to a 2-2 draw in 2018 in the group stage.
Japan: As Group E winners, the Samurai Blue will face Group D runners-up Croatia on Monday.