Despite a 4-2 victory over Costa Rica, who is also out of the Qatar tournament, Germany has been ousted from the World Cup at the group stage for the second time in a row.
At the Al Bayt Stadium, Die Mannschaft scored through Serge Gnabry, Kai Havertz (2), and Niclas Fullkrug. The former gave his country the lead until Yeltsin Tejeda and Juan Pablo Vargas turned the match around.
Germany fulfilled their obligations thanks to Havertz's brace and Fullkrug's tap-in, but the Asian nation qualified in first position from Group E as a result of Japan's unexpected victory over Spain, and La Roja edged the four-time champions on goal differential.
At the Al Bayt Stadium, Germany started off strong as expected. As the game progressed the Germans were looking in control with 10 minutes on the clock, Serge Gnabryhead into the far corner of the Cost Rica goal to give the Germans an early lead.
Following Gnabry's goal, Die Mannschaft continued to dominate, although rarely making any crucial adjustments. However, Navas had to be alert to save Leon Goretzka's point-blank header in the 14th minute.
Gnabry nearly added a second goal in the 40th minute, but his turn and shot went just wide of the far post. Just before halftime, Costa Rica was given a wonderful opportunity to send the net tumbling.
Keysher Fuller, who scored the winning goal against Japan at the weekend, was able to go through one-on-one with Manuel Neuer because to two defensive errors by Raum and Antonio Rudiger, but the 36-year-old instinctively placed a hand up to deflect the right-attempt back's over the bar.
News that Japan had defeated Spain caused faces of agony to appear on the players in black and white as their team tried to build on a slim one-goal advantage during a very quiet start to the second half on the pitch.
The situation quickly became worse for Die Mannschaft as Tejeda scored his first goal for his country after Neuer was unable to hold on to Kendall Waston's header in the 58th minute.
Costa Rica's goal was appropriate compensation for their quick breaks throughout the game, but Germany came dangerously close to taking the lead again in the 61st minute when Musiala hit the post from a tight angle before volleying the follow-up over the top and Rudiger's close-range attempt also hit the woodwork.
Musiala's spectacular curling strike off the post in the 67th minute was Germany's third bad bounce, and all those missed opportunities would come back to haunt Flick's team in the 70th.
In a sea of bodies, Waston met a floating free kick at the back post and headed back into the danger zone. Centre-back Vargas's lightest touch allowed the ball to find its way into the goal.
The jubilation of the Costa Ricans would only last three minutes, however, when Havertz, a Chelsea attacker, scored after receiving the ball in the area to tie the game. In the 76th minute, Fullkrug could have given Germany the lead, but Navas made an incredible save from just inches away.
However, Havertz only needed one opportunity to give Germany the lead back in the 85th minute. Gnabry picked out his compatriot's move to the back post before Havertz tapped home into the far corner. Luis Fernando Suarez's team continued to pose a threat on every push forward.
In the 89th minute, substitute Fullkrug scored from close range to extend Germany's lead. The goal was initially disallowed for offside before being allowed after a VAR review, but Flick's team still needed Spain to help them knock Japan out of the top two.
However, Japan held on to defeat Spain, ensuring their spots in the knockout roundsat the expense of those at the Al Bayt Stadium, sealing the fates of Germany and Costa Rica before the final whistle.
For more Sports updates please follow us on Twitter