Germany suffered a second consecutive World Cup group-stage exit despite a thrilling 4-2 win over Costa Rica on a night of sensational Group E drama in Qatar
After collecting just one point from their opening two games, four-time tournament winners Germany - who were also eliminated at the group stage in 2018 - knew only a victory at Al Bayt Stadium on Thursday night would give them any chance of reaching the last 16
But despite eventually securing the result they needed, Hansi Flick's side were knocked out of the tournament on goal difference after  in Group E's other fixture
Serge Gnabry's early header (10) appeared to have set Germany on the way to a comfortable three points, but a second-half strike from Yeltsin Tejeda (58) and a Manuel Neuer own goal (70) remarkably put Costa Rica in pole position to qualify for the last 16 and raised the incredible possibility of both Germany and Spain going out
But the drama did not end there
 Chelsea forward Kai Havertz came off the Germany bench and turned the game on its head again with a clinical brace (73, 85) before fellow substitute Niclas Fullkrug made it 4-2 in the 89th minute
The victory was not enough though as the 2014 world champions finished third in Group E behind runners-up Spain and winners Japan
Thursday's game at Al Bayt Stadium also made history as France's Stephanie Frappart became the first woman to referee a match at the men's World Cup
 Assistants Neuza Back from Brazil and Karen Diaz Medina from Mexico made it an all-female on-field team
Japan's win over Spain sees them finish top of Group E with six points, meaning they will face Group F runners-up Croatia in the last 16 on Monday, with kick-off at 3pm
Spain finish second on four points, ahead of Germany on goal difference, and will face Group F winners Morocco in the last 16 on Tuesday, kick-off at 3pm
Costa Rica finish bottom of Group E on three points and, like Germany, exit the competition
Germany's night started in encouraging fashion when Gnabry made the back of the net ripple on 10 minutes
 David Raum was the creator on Germany's left-hand side, and his cross was expertly steered into the far corner by the Bayern Munich forward
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 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 powerful effort 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
 And it would get worse for the Germans when Tejeda scored his first international goal on 58 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
 Neuer was deemed to get the final touch when 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 Fullkrug's pass three minutes later
 And then, with five minutes remaining, Havertz had his second, turning home Gnabry's cross 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 that 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
 said: "In the first half I was disappointed and very angry at my team and how we allowed the opponent to come back
 We wanted to score three or four goals in the first half but then we made mistakes
"But the tournament was not decided today for us
 We did not have any efficiency at this tournament and that is why we were eliminated
"If you know me and my team, I know we can get up quickly and recover from that
 Now we need to assess our work during the World Cup and head in a different direction
 This is the next step we are going to undertake and we will do that very soon
"For the future of German football, we need to train differently
 We will work on a future that is very important and very decisive, and we will see how we can implement our idea
"For years we've been talking about new goalkeepers and wingbacks, but what was always good was that we defended well
 We need the basics to be right
" told German broadcaster : "It's an absolute disaster
 I don't know what happens next
"If this was my last game, then I would like to say a few words to the German football fans
 It was an enormous pleasure, dear people
"We experienced great moments
 I tried to leave my heart on the pitch in every game
"It is unbelievably bitter for us because our result would have been enough
 It's a feeling of powerlessness
" said: "In this World Cup, we showed what Costa Rica are
 I am very calm that we can leave with our heads high
 I am so proud of this squad, they are so special
 I am so proud of them, more than ever before
"I leave with a feeling of sadness, not because we lost but that we are leaving
 It's tough to experience this as a coach, even more so in the World Cup
"The most important thing is there is so much room for improvement
 In the next four years there are things we need to change, but we need to build on everything we have done in the past few years
"The goal that ended up sending Germany home
<PAD>
<PAD>
<PAD>
<PAD>
<PAD>
<PAD>
<PAD>
<PAD>
<PAD>
<PAD>
<PAD>
<PAD>