Germany have been knocked out of the World Cup at the group stage for the second successive time despite a 4-2 win over Costa Rica, who are also eliminated from the Qatar tournament.
Die Mannschaft struck through Serge Gnabry, Kai Havertz (2) and Niclas Fullkrug at the Al Bayt Stadium, with the former putting his nation a goal to the good before Yeltsin Tejeda and Juan Pablo Vargas turned the tie on its head.
Havertz's brace and Fullkrug's tap-in meant that Germany did what was required of them, but Japan's shock win over Spain saw the Asian nation qualify in first place from Group E, with La Roja pipping Germany to second spot on goal difference on a debilitating evening for the four-time champions.
As expected, Germany exploded out of the blocks at the Al Bayt Stadium, with Keylor Navas getting down low to keep out the effervescent Jamal Musiala in the third minute.
Thomas Muller then inexplicably headed wide after being picked out by Joshua Kimmich in the ninth minute, but one of his other Bayern Munich teammates showed him how it is done not long after.
With 10 minutes on the clock, Musiala released David Raum down the left, and the RB Leipzig defender crossed for Gnabry to head across goal into the far corner.
Die Mannschaft continued their dominant period following Gnabry's opener, albeit while rarely creating many gilt-edged changes, although Navas had to be alert to keep out a point-blank header from Leon Goretzka in the 14th minute.
Gnabry came close to a second in the 40th minute but fired just wide of the far post on the turn, but it was Costa Rica who were presented with a golden chance to make the net ripple just before the break.
A pair of defensive lapses from Raum and Antonio Rudiger allowed Keysher Fuller - the scorer of the winner against Japan at the weekend - to go through one-on-one with Manuel Neuer, but the 36-year-old instinctively stuck a hand up to tip the right-back's effort over the bar.
During a rather muted start to the second half on the field, news filtered through of Japan taking the lead against Spain, leading to faces of anguish from those in black and white as their side struggled to build on a slender one-goal lead.
Things soon went from bad to worse for Die Mannschaft, as in the 58th minute, Neuer failed to hold on to Kendall Waston's header, and Tejeda was on hand to fire home from close range for his first international goal.
Costa Rica's strike was just reward for their quick breaks during the contest, but Germany were very nearly back in front in the 61st minute, as Musiala struck the post from a tight angle before volleying the follow-up over the top, and Rudiger then crashed a close-range effort of his own off the woodwork.
It was third time unlucky for Germany in the 67th minute as Musiala's magnificent curling strike came back off the post, and all of those wasted opportunities would bite Flick's side again in the 70th minute.
Waston met a floated free kick at the back post and headed back into the danger area, and the slightest of touches from centre-back Vargas in a sea of bodies directed the ball into the back of the net.
The Costa Ricans' joy would last all of three minutes, though, as the ball fell to Havertz in the box for the Chelsea attacker to slot home to level the scores, and Fullkrug should have put Germany ahead in the 76th minute, but Navas produced a breathtaking save from less than a yard out.
Luis Fernando Suarez's side continued to pose a threat on every foray forwards, but Havertz needed just one chance to put Germany back ahead in the 85th minute, with Gnabry picking out his compatriot's run to the back post before Havertz tapped home into the far corner.
Substitute Fullkrug then tapped in from close range to double Germany's lead in the 89th minute, with the goal initially chalked off for offside before being given upon a VAR review, but Flick's side still needed a favour from Spain to force Japan out of the top two.
However, Germany and Costa Rica's fates were sealed before the final whistle as Japan held on for victory over Spain, which confirmed their places in the knockout rounds at the expense of those at the Al Bayt Stadium. body check tags ::