Germany were dumped out of the World Cup despite a 4-2 win over Costa Rica after Japan sprung a major surprise in recording a 2-1 victory against Spain to top Group E.
Spain and Germany entered the final round of Group E matches as favourites to progress to the last 16, but Japan – who already beat Germany earlier in the tournament – ripped up the script.
Despite Spain taking the lead through Alvaro Morata, Japan hit back with two second-half goals to seal another famous win.
Although Germany battled back from a 2-1 half-time deficit to claim a 4-2 victory, they were eliminated on goal difference after finishing level on four points with Spain.
Japan top the group with six and Costa Rica finished last with three. Japan now face Croatia in the last 16, while Spain take on Morocco.
Spain had looked in total control at half-time against after taking the lead through Morata’s early header.
Japan, though, turned things around in the space of five minutes at the start of the second half when substitute Ritsu Doan equalised before Ao Tanaka netted another – which was allowed to stand following a lengthy VAR review after the ball had appeared to have gone out of play.
Japan, who had opened their campaign with a shock win against Germany but then were beaten by Costa Rica, almost snatched an early lead when Junya Ito fired into the side-netting.
Spain went in front in the 11th minute when Morata headed in a cross from the right by Chelsea captain Cesar Azpilicueta for his third goal of the tournament.
As news came through of Germany scoring an early goal against Costa Rica, the Samurai Blue fans became subdued, mirroring their team on the pitch.
There was a rare moment of action in the Spain penalty area just after the half-hour mark when goalkeeper Unai Simon dwelt on the ball a bit too long, and Daizen Maeda almost capitalised.
Spain continued to dominate possession and although it looked only a matter of time before they would open up the Japan defence again, there were no more goals ahead of the break.
Japan coach Hajime Moriyasu made a double change for the second half, with Brighton midfielder Kaoru Mitoma and Doan sent on for Yuto Nagatomo and Takefusa Kubo. Spain full-back Azpilicueta was replaced by Dani Carvajal.
The changes made an instant impact as Japan equalised three minutes after the restart.
Spain were caught trying to play out from the back as Freiburg winger Doan picked up possession just outside the penalty area and drilled a shot past Simon, who could only palm the ball into the top left corner.
Japan completed another remarkable turnaround when they took the lead in the 51st minute.
Doan was again involved as his low cross went right through the six-yard box and was cut back in by Mitoma from the goal-line at the far post, with Tanaka bundling the ball home.
A lengthy VAR check followed and officials eventually ruled the whole of the ball had not crossed the line – a decision which sparked pandemonium among the now jubilant Samurai Blue faithful.
Spain boss Luis Enrique reacted by replacing Morata with Marco Asensio and sending on Ferran Torres for Nico Williams.
Japan created another good opening with 20 minutes left.
Mitoma broke down the left and clipped a pass back across to the edge of the penalty area where Takuma Asano arrived at pace, but the Bochum forward could only skew his shot wide.
Spain pushed for an equaliser and Japan goalkeeper Shuichi Gonda made a smart reaction save to push away a low drive from Asensio before then collecting Dani Olmo’s angled shot at the near post as Japan celebrated another remarkable victory and a place in the last 16.
Although Germany came from behind to win, with Chelsea’s Kai Havertz scoring a quickfire double, Japan’s comeback victory saw Hansi Flick’s side heading for the exit.
Germany’s night started in encouraging fashion when Serge Gnabry made the back of the net ripple after just 10 minutes.
David Raum was the creator on Germany’s left-hand side, and his cross was expertly steered into the far corner by Gnabry.
Germany were dominant and Leon Goretzka almost scored at the back post only for his header to be turned away by Keylor Navas, before the impressive Jamal Musiala masterfully tip-toed his way into the Costa Rica area but pushed his effort wide.
Despite being in complete control, Germany had Manuel Neuer – making his 19th outing at the World Cup to become the goalkeeper with the most appearances in the history of the competition – to thank for keeping them ahead at the interval.
Raum and Antonio Rudiger failed to deal with a long ball, allowing Keysher Fuller through, but his blast on goal was brilliantly palmed over by the veteran German stopper.
At half-time – and with Spain 1-0 up following Alvaro Morata’s strike – Germany were on course for a last-16 tie against Morocco. But that dramatically changed within minutes of the restart.
It would get worse for the Germans when Yeltsin Tejeda scored his first international goal after 57 minutes to draw Costa Rica level.
In a frenetic response, Germany hit Navas’ left-hand post on three occasions – twice by Musiala and also by Rudiger – before Costa Rica did the unthinkable and scored a second.
Juan Pablo Vargas bundled the ball home from a free-kick to send Costa Rica wild and on the verge of following Japan into the knockout stages, with Spain joining Germany in departing the competition.
But a topsy-turvy night took another twist when substitute Havertz hauled Germany level after he converted Niclas Fullkrug’s cross three minutes later.
And then, with five minutes remaining, Havertz had his second, turning home Gnabry’s pass to restore Germany’s lead.
Fullkrug then survived a VAR check to net his side’s fourth before 10 minutes of stoppage time ensued, with Germany hoping Spain could rescue a late equaliser against Japan – a result which would see them progress.
But Japan held on to seal a famous win against Spain and send Germany out of the World Cup – four years after they fell at the first hurdle as defending champions in Russia – on a dark night for one of the world’s great football superpowers.