DOHA (Reuters) -- Japan staged their second stunning turnaround of the World Cup to beat 2010 champions Spain 2-1 on Thursday to surge into the last 16 along with their beaten opponents and dump Germany out in one of the greatest nights for the Samurai Blue.
Staring at the prospect of an early World Cup exit at half-time, Japan set up a round of 16 clash with Croatia - the beaten finalists in 2018 - while Spain had to rely on goal difference to edge out Germany. They now face Morocco.
Japan's turnaround was built on coach Hajime Moriyasu's revamp of his side at the break when they were losing 1-0.
On came Ritsu Doan and within three minutes he hammered in a shot from the edge of the box which Spain goalkeeper Unai Simon - whose poor clearance had created the danger in the first place - could not keep out.
With Japan's noisy travelling fans barely catching their breath, Ao Tanaka scored from point-blank range in the 51st minute, bundling the ball in after the other halftime sub Kaoru Mitoma cut the ball back from the byline.
The VAR system took several minutes to confirm the ball had not gone out before Mitoma's touch. Replays suggested that a sliver of the ball had not crossed the line and the verdict sparked wild celebrations similar to the scenes that followed Japan's 2-1 comeback win over Germany last week.
"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," he told reporters.
Spain had looked in total control in the first half but suddenly they were the ones clinging to their chance of staying in the World Cup as Costa Rica and Germany battled out their own drama in the other Group E match.
Germany won 4-2, a margin of victory that was not big enough to keep them in the tournament.
Alvaro Morata had put the Spaniards ahead when he scored his third goal in three games in Qatar, rising above Japan's defence to power a header past goalkeeper Shuichi Gonda in the 11th minute after a cross by his former Chelsea team mate Cesar Azpilicueta.
In the dying minutes, winger Dani Olmo came closest to a leveller but his angled shot was easily gathered by Gonda.
The breathtaking win for Japan meant they topped Group E, followed by Spain in second who finished ahead of third-placed Germany only on goal difference.
Japan fans were in tears at the end of the match which gives them a chance to go further than they ever have before at a World Cup. Japan have previously reached the last 16 on three occasions and lost each time.
"There were many fans who came all the way from Japan, as well as those that stayed behind in Japan," coach Moriyasu said. "We are gifting this win to the people of Japan and we are very happy about it."
Spain coach Luis Enrique, who made five changes to his starting side to give first-choice players a rest, was far from happy.
"We didn't have any danger in the first half, then in the second half, I told them during halftime to be cautious, because they would attack in the second half, they don't have anything to lose," he said. "I have nothing to celebrate."
Japan defender Kou Itakura was shown his second yellow card of the tournament and will be suspended for the Croatia match.