Going into the halftime break 1-0 down and on course for an early World Cup exit, Japan coach Hajime Moriyasu reorganised his side to give them more attacking bite and within six minutes the Samurai Blue were ahead.
Ritsu Doan, one of the substitutes, hammered in a shot from the edge of the box in the 48th minute that Spain goalkeeper Unai Simon - whose poor clearance had created the danger - could not keep out.
Three minutes later, Ao Tanaka scored from close range 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, sparking wild celebrations by Japan and their fans when the verdict came in.
Spain had looked in total control in the first half but were suddenly 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.
Alvaro Morata had put the Spaniards ahead when he scored his third goal in three games in Qatar, rising high above Japan's defence to power a header past goalkeeper Shuichi Gonda in the 11th minute after a cross by Cesar Azpilicueta.
The breathtaking 2-1 win for Japan - who also beat Germany 2-1 after going behind in their opening match of the tournament - meant they topped Group E, followed by Spain in second who finished ahead of third-placed Germany only on goal difference.
Japan will face Croatia in the last 16 while Spain will meet Morocco.