Japan scored a controversial winner as they staged another extraordinary World Cup comeback to top Group E ahead of Spain and reach the last 16.
Ao Tanaka claimed Japan’s second goal after the Video Assistant Referee ruled the ball had not gone out of play before substitute Kaoru Mitoma crossed from the byeline, even though on first viewing it appeared to have done so.
It proved the decisive moment in a stunning performance against a Spain side who were fortunate to join Japan in the knockout stage.
In a tournament that has been full of shocks, this was perhaps the most dramatic night so far, with Spain left relying on the result of Germany’s game with Costa Rica for them to progress.
All four teams had started the evening with a chance of progressing but with Spain needing only a draw to qualify, they were not expected to need any favours from elsewhere.
La Roja were cruising at the break after Alvaro Morata headed home Cesar Azpilicueta’s cross from close range, but Japan would quickly turn the game – and the whole of Group E – on its head.
The Samurai Blue sent on substitutes Ritsu Doan and Mitoma at half-time and within six minutes of the restart, they had both made a huge impact.
Doan equalised with a fierce shot from the edge of the area and, only 142 seconds later, Mitomo retrieved Doan’s low cross and found Tanaka to bundle home from close range in dramatic circumstances.
The goal was initially ruled out by the assistant referee who signalled Doan’s cross had gone out of play, but the VAR ruled Mitomo had reached it in time.
To the naked eye, it appeared over the line, but the decision was made that the curvature of the ball was hanging over the chalk, to the delight of Japan’s players.
Things got even worse for Spain when Costa Rica took a surprise lead over Germany moments later because at that stage they were heading out.
The big screens at Khalifa International Stadium showed the updated group table, to the disbelief of the Spain supporters, and pretty much everyone else here too.
Luis Enrique’s side were in big trouble and were showing few signs of knowing how to rescue themselves, with only Germany’s late rally sending La Roja through as group runners-up on goal difference.
Japan’s reward for a remarkable performance is a tie with Croatia in the last 16, while a relieved Spain side will face Morocco.
This stadium was the setting for Japan’s stunning victory over Germany in their opening game at this tournament, and it clearly inspired them again.
The high-pressing style that undid Germany had unsettled Spain at times in the first half but there were still few signs of what was to come.
Morata appeared set to be the story of the night when he netted his third goal of this tournament, equalling Telmo Zarra’s record from 1950 for scoring on his first three World Cup appearances for Spain.
Japan had other ideas, and tore into Spain from the start of the second half, again refusing to let them 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 got two hands to but could not keep out.
That sent Japan’s followers wild but more excitement would quickly follow thanks to Tanaka, albeit after a lengthy VAR check.
Spain still had virtually the whole of the second half to stage a recovery, but they lacked either energy or ideas – or both – and rarely threatened Shuichi Gonda in the Japan goal.
It was only the news of Germany’s comeback that allowed the Spain fans to relax in the closing minutes, with the goals that La Roja scored in their opening 7-0 win over Costa Rica proving decisive in sending them through.
Morocco edged past Canada to top Group F and reach the World Cup knockout stage for the first time in 36 years.
The Atlas Lions, whose previous trip to the last 16 came in 1986, finished above 2018 finalists Croatia, while Belgium – ranked second in the world – are out after finishing third in the group.
A tough test awaits in the next round as they come up against 2010 champions Spain on Tuesday (15:00 GMT).
Morocco went ahead after just four minutes courtesy of a goalkeeping howler from Milan Borjan.
The Canada keeper sprinted out of his goal but passed the ball straight to Hakim Ziyech, who lofted a cool finish into an open net from 30 yards.
The north African side doubled their advantage through Youssef En-Nesyri’s well-taken goal as he controlled Achraf Hakimi’s superb pass before firing in.
But sloppy defending gave Canada a route back into the game when West Ham’s Nayef Aguerd stuck a boot out to divert the ball into his own net – the 100th goal scored at this World Cup.
Knowing if they avoided defeat they would go through, Morocco were happy to sit back and soak up pressure in the second period.
But Atiba Hutchinson came close with a header that rattled the crossbar and dropped down onto the line as Canada’s campaign ended without a point.
Morocco didn’t win a game at the last World Cup in Russia but has turned on the style this time, becoming the second African team after Senegal to make it through.
Cameroon plays Brazil (19:00 GMT) and Ghana faces a grudge match against Uruguay in the final round of group fixtures on Friday to determine whether they can make it a total of four from the continent.
Morocco manager Walid Regragui has a team that is full of players with big-game experience.
Chelsea’s Ziyech, who has been off the boil for his club, has found form for his country and grabbed the opener, which was gifted to him by Borjan.
Hakimi and En-Nesyri, a regular scorer in the Champions League, combined for the second as the Sevilla player latched on to the Paris St-Germain full-back’s pin-point pass before smashing in.
He could have had a second but a stunning volley was ruled out for offside.
Before the game, Morocco had kept a clean sheet in six matches, five of them under Regragui, but that record went when Aguerd poked into his own net before the break.
Canada probed for a way back in the second half and although Hutchinson came close with a header that hit the woodwork, they failed to produce a shot on target in the match.
Star man Alphonso Davies stabbed a chance wide, meaning his side have now lost all six of their World Cup games, a feat only achieved previously in El Salvador.