Ao Tanaka scored the winning goal from close range early in the second half. It took about two minutes for video review officials to confirm the ball hadn't gone out of bounds before the goal.
Alvaro Morata scored first for Spain in the 11th minute at Khalifa International Stadium, but Japan rallied after halftime. Ritsu Doan equalized in the 48th with a left-footed shot from outside the box, and Tanaka added the second one three minutes later.
Germany were eliminated from the tournament even with a 4-2 win over Costa Rica in the other Group E match. A victory by Costa Rica would have eliminated Spain.
Check the 2022 World Cup schedule and groups standings.
(For more sports news and updates, follow Ahram Online Sports on Twitter at @AO_Sports and on Facebook at AhramOnlineSports.)