On a thrilling night in Qatar, Japan defeated Spain 2-1 in the other group game, ending Germany’s World Cup journey for the second time in a row.
Germany has been eliminated from the World Cup after a win over Costa Rica was insufficient to secure a place in the last 16.
In their final group game, Hansi Flick’s team came from behind to defeat Costa Rica 4-2.
Takuma Asano of Japan celebrates qualifying
Japan’s winning goal, which eliminated Germany, was somewhat contentious, with some believing the ball went out of play before Kaoru Mitoma crossed it to Ao Tanaka, who scored.
However, after a lengthy VAR review, the goal was ruled in Japan’s favor, with officials stating that the entire ball had not crossed the line.
The results place Japan atop Group E, with Spain coming in second and Costa Rica failing to qualify.
Kaoru Mitoma of Japan in action as the ball appears to cross the line before Ao Tanaka of Japan scores their second goal.
Germany led 1-0 at halftime thanks to an early goal by Serge Gnabry, but Costa Rica came back to win 2-1, knocking Germany and Spain out of the competition.
However, Kai Havertz equalized and then added a third goal to put Germany back in front, before Niclas Fullkrug added a fourth.
In the last 16, Japan will face Group F runners-up Croatia, while Spain will face Group F winners Morocco.