Japan have qualified for the last 16 of the World Cup as Group E winners after a shock 2-1 win over Spain that saw the latter go through but Germany crash out.
Alvaro Morata's header gave La Roja the lead in the first half, but two quickfire goals from Ritsu Doan and Ao Tanaka after the break were enough for Hajime Moriyasu's side to seal Japan's place in the knockout stages.
Spain finish as group runners up on four points by virtue of their superior goal difference over Germany, who came from behind to beat Costa Rica 4-2.
Japan will play Croatia on December 5 while runners-up Spain will take on Morocco on December 6.
Spain dominated possession from the first minute with Japan struggling to make any inroads aside from Junya Ito's driven strike which crashed into the side netting in the eighth minute.
So it was little surprise when Morata got his third World Cup goal of the campaign in the 12th minute. He nodded in a cross from Cesar Azpilicueta at point-blank range to get his 30th Spain goal.
Japan boss Moriyasu brought Doan and Kaoru Mitoma into the game at half-time and both were pivotal in their two goals.
In the 48th minute, Unai Simon's poor clearance was intercepted by Japan before Doan's piledriver from 25 yards out could only be batted into the top corner by the Athletic Bilbao goalkeeper.
Five minutes later and Japan got a shock second when Mitoma just about kept the ball in play on the byline before squaring for Tanaka to prod into the net. The ball was originally adjudged to go out of play, but the decision was overturned by VAR.
Japan sat deeper as Spain looked to break down their compact defence. But Spain were guilty of overplaying and Japan were able to seal a historic victory over the 2010 World Cup winners.