Spain lost 2-1 to Japan in their final group match at the FIFA World Cup in Qatar on Thursday, but still progress to the last 16, together with their opponents, after Costa Rica lost to Germany in the other group match.
Spain lost to Japan unexpectedly on Thursday evening, and after placing second behind the Samurai Blue in Group E, will play Morocco in the round of 16.
Alvaro Morata, one of the five additions Luis Enrique made to the lineup that drew 1-1 with Germany on Sunday, gave La Roja the lead with a header after 12 minutes.
Spain appeared to be in control after a pretty one-sided first half, but Japan scored twice in a row to completely change the game after the break.
Japan came from behind to win it, just like they did in their first game against Germany. Ritsu Doan and Ao Tanaka scored goals after 48 and 51 minutes, respectively.
And the second of those was somewhat contentious because it appeared that the ball had crossed the goal line, but a VAR review revealed that it had not fully crossed the line before Kaoru Mitoma redirected the ball across goal for Tanaka to score.
In any case, the goal stood and Germany went out as Spain were knocked down to second in Group E.
Luis Enrique's side will meet Group F winners Morocco in the last 16, while Japan will play Croatia in the next round.