Spain staggered through to the World Cup last 16 on Thursday despite a 2-1 defeat by Japan, which remarkably won Group E on a roller-coaster night when four-time champion Germany was eliminated.
At the end of the first half, Spain and Germany were through. Then, for three minutes, Japan and Costa Rica were positioned to advance.
Japan secured another incredible triumph after beating Germany in their opening game, with the progression from the "group of death" one of the greatest achievements in the country's soccer history.
For a few heart-stopping minutes Spain was poised to head out when Costa Rica was leading in the other game, but the 2010 champions squeezed through after Germany did them a favor by coming back to beat Los Ticos 4-2.
Spain and Germany ended on four points each, but La Roja progressed on goal difference in second place.
Alvaro Morata sent Spain ahead early on with a towering header, but Japan dramatically struck back at the start of the second half.
Ritsu Doan and Ao Tanaka's goals saw the Japanese mount a sensational comeback, just as they did against Germany.
Tanaka's goal was highly controversial after the officials relied on VAR to decide Kaoru Mitoma had kept the ball in play by a hair's breadth to set him up.
Spain struggled to create chances against a staunch Japanese defense in the final stages, with the Asian team aware that conceding another goal would see it eliminated.
Japanese players collapsed at full-time in celebration, exhausted and elated, while the substitutes raced on to celebrate.
Spain will face Morocco in the last 16, while Japan plays Croatia.
"It's not like we have re-written history, but we've overcome a big wall," said Doan, who netted against both Spain and Germany. "I think it's the result of fighting as a team."
Both sides made five changes, with Luis Enrique bringing in Morata to lead the line, and handing Alejandro Balde and Nico Williams their full debuts.
Japan started with five at the back, hoping to keep Spain at bay, but pressed high in attack to try and capitalize on any mistakes.
Spain are sometimes guilty of glaring defensive errors and Pau Torres exemplified that with a sloppy touch which led to Japan's first chance, fired into the side-netting by Junya Ito.
Morata sent La Roja ahead in the 12th minute with a towering header from Cesar Azpilicueta's cross, rekindling the connection which occasionally worked at Chelsea after the striker's arrival in 2017.
Japan coach Hajime Moriyasu replaced the ineffective Takefuso Kubo and Yuto Nagatomo at the break with Mitoma and Doan, and it paid instant dividends.
Balde lost the ball under pressure and from the edge of the box winger Doan hammered past Unai Simon, who might have done more to keep it out.
Three minutes later and Japan had turned the game on its head, Tanaka bundling home from on the goalline after Mitoma had miraculously kept the ball in play as he crossed it -- according to VAR.
Spain was shell-shocked and struggled to react, with Luis Enrique making a raft of substitutions to try and regain control.
The coach, alone and perched on the edge of his technical area, was unaware that for a few moments his team faced elimination, when Costa Rica took the lead against Germany in the other game to go 2-1 ahead.
Spain survived, progressing thanks to their superior goal difference, on a night that was far more terrifying than it had anticipated -- and Japan secured a hugely unlikely achievement.
"We are through, that's the important thing," Morata said. "Another slip-up and we are going home. We have to continue with confidence in ourselves (but) it cannot happen again because we'll be going home."
© Agence France-Presse