Japan scored a massive upset over Spain in the FIFA World Cup to eliminate Germany from the competition in the process.

Just days out from beating the Germans, Japan condemned Spain to the same fate coming from behind to secure a 2-1 win at Khalifa International Stadium, which makes their loss to Costa Rica that much more puzzling.

There was no indication of an upset as Spain looked in complete control after Alvaro Morata headed the 2010 champions in front after just 11 minutes and they finished the first half with almost 80% possession.

But Japan, who needed at least a draw to have a chance of progressing, roared to life at the start of the second half, scoring two goals in the first six minutes.

Substitute Ritsu Doan drew them level in the 48th minute after Spain failed to play out from the back before Ao Tanaka scored their second from close range.

The goal was full of controversy as the ball appeared to go over the line before Kaoru Mitoma had crossed for Tanaka but the goal stood even after a VAR check.

With Japan winning, Spain were lucky to advance to the knockout stages of the FIFA World Cup after Germany beat Costa Rica 4-2.

Serge Gnabry, Kai Havertz (2) and Niclas Fullkrug scored for the Germans while Yelstin Tejeda and Manuel Neuer’s own goals were responsible for Costa Rica’s.

The 2014 champions exited the competition from group stages for a second consecutive time on goal difference after both Spain and they finished with 4 points from three games.

Japan will now face Croatia while Spain takes on Morocco in the next round.