Alvaro Morata kept up his impressive goal scoring form for Spain, powering a header into the back of the net after a cross from Cesar Azpilicueta.
However, Spain were unable to hold onto the lead as Ritsu Doan fired Japan level with an impressive goal, cutting in on his left foot to fire past Unai Simon.
And Japan took the lead just moments later, with VAR giving the goal after the linesman deemed the ball to have been out of play in the build-up.
Therefore, Japan qualified from Group E top of the table, with Spain finishing second despite Germany’s victory over Costa Rica.
Japan have picked up another major scalp in the 2022 World Cup with a 2-1 win over Spain.
The Furia Roja dominated the first half and opened the scoring after 12 minutes when Cesar Azpilicueta set the unmarked Alvaro Morata up with a wonderful cross that saw the striker head it past Suichi Gonda.
But the Samurai Blue completed a sensation comeback int he second half on 48 minutes.
It started with Ritsu Doan who beat Unai Simon with a stunning long-range shot after Alejandro Balde gave the ball away and the goalkeeper couldn’t hold on to the ball.
And Japan turned things around just three minutes later when Kaoru Mitoma saved the ball from foing out for a free-kick and turned it around to Ao Tanaka who scored in front of open goal.