Five goals were scored in the second half as Germany beat Costa Rica 4-2 in their Group E finale but it was not enough and they are out of the World Cup.
Germany slipped to defeat against Japan and drew to Spain in their first two group games.
This meant they needed to beat Costa Rica and hope that Japan fail to beat Spain in the other Group E finale to go through.
Kai Havertz’s brace helped Germany come from behind to beat Costa Rica, but Japan produced a comeback of their own versus Spain to dump Hansi Flick’s side out of the competition.
Costa Rica finish bottom of the group, with Japan and Spain booking their spots in the next round.
Germany’s night started in encouraging fashion when Serge Gnabry made the back of the net ripple after just 10 minutes.
David Raum was the creator on Germany’s left-hand side, and his cross was expertly steered into the far corner by Gnabry.
Germany were dominant and Leon Goretzka almost scored at the back post only for his header to be turned away by Keylor Navas, before the impressive Jamal Musiala masterfully tip-toed his way into the Costa Rica area but pushed his effort wide.
Despite being in complete control, Germany had Manuel Neuer – making his 19th outing at the World Cup to become the goalkeeper with the most appearances in the history of the competition – to thank for keeping them ahead at the interval.
Raum and Antonio Rudiger failed to deal with a long ball, allowing Keysher Fuller through, but his blast on goal was brilliantly palmed over by the veteran German stopper.
At half-time – and with Spain 1-0 up following Alvaro Morata’s strike – Germany were on course for a last-16 tie against Morocco. But that dramatically changed within minutes of the restart when Ritsu Doan and Ao Tanaka fired Japan into the lead.
It would get worse for the Germans when Yeltsin Tejeda scored his first international goal after 57 minutes to draw Costa Rica level.
In a frenetic response, Germany hit Navas’ left-hand post on three occasions – twice by Musiala and also by Rudiger – before Costa Rica did the unthinkable and scored a second.
Juan Pablo Vargas bundled the ball home from a free-kick to send Costa Rica wild and on the verge of following Japan into the knockout stages, with Spain joining Germany in departing the competition.
But a topsy-turvy night took another twist when substitute Havertz hauled Germany level after he converted Niclas Fullkrug’s cross three minutes later.
And then, with five minutes remaining, Havertz had his second, turning home Gnabry’s pass to restore Germany’s lead.
Fullkrug then survived a VAR check to net his side’s fourth before 10 minutes of stoppage time ensued, with Germany hoping Spain could rescue a late equaliser against Japan – a result which would see them progress.
But Japan held on to seal a famous win against Spain and send Germany out of the World Cup – four years after they fell at the first hurdle as defending champions in Russia – on a dark night for one of the world’s great football superpowers.
READ MORE: World Cup player stats… Goals, assists, shots, xG, key passes and more as four groups sewn up