Germany crashed out of the group stage of the World Cup for the second time in succession despite a 4-2 victory against Costa Rica.

Following a shock defeat to Japan and a scrappy draw against Spain, the four-time world champions knew only victory at Qatar’s Al Bayt Stadium on Thursday night – and failure by Japan to beat Spain in Group E’s other fixture – would keep their dreams alive.

But although Germany came from behind to win, with Chelsea’s Kai Havertz scoring a quickfire double, Japan’s 2-1 comeback victory against Spain saw Hansi Flick’s side heading for the exit.

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.

Germany’s Serge Gnabry scores Serge Gnabry scored the opening goal of the game for Germany (Matthias Schrader/AP)

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 scores for Costa Rica Juan Pablo Vargas scored Costa Rica’s second to put his side ahead (Adam Davy/PA)

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.