Germany crashed out of the World Cup on a remarkable night of drama despite victory over Costa Rica at Al Bayt Stadium.
Japan’s 2-1 win against Spain would have eliminated Germany anyway, but Hansi Flick’s side needed three points to give themselves any chance of reaching the last 16.
They completed their part of the bargain, but it was to no avail as the four-time champions finished third in the group and were eliminated on goal difference.
Germany were in complete control and in front through Serge Gnabry’s 10th-minute header only for Costa Rica to turn the match on its head to raise the remarkable possibility of Spain also going out.
Yeltsin Tejeda equalized after Germany keeper Manuel Neuer pushed out Kendall Waston’s header in the 58th minute before Juan Pablo Vargas bundled in a second 12 minutes later.
At that stage, Costa Rica were going through with Japan, and Spain were heading for the exit.
It would have capped one of the most astonishing nights in World Cup history, but Germany substitute Kai Havertz restored a measure of normality to proceedings with two clinical finishes to extinguish Costa Rica’s hopes of springing a major shock.
Substitute Niclas Fullkrug added a last-minute fourth to confirm victory, but it will be of no consolation as Germany failed to reach the knockout stage of the tournament for the second time in succession.