After a set of results that also saw Japan beat Spain 2-1, Germany ultimately finished third in Group E and exited the tournament in the group stages.
They finished level on points with second place Spain but missed out due to goal difference.
Serge Gnabry opened the scoring on 10 minutes with a fine headed effort to give Germany a 1-0 interval lead.
It looked good at that moment with Spain, also winning 1-0 at the break, set to join Germany in the Round of 16 as the half-time whistle blew.
But, with Japan turning things around with a quickfire double early in the second half against Spain, it became an uphill battle for Hansi Flick’s side.
The difficulty level was raised even further when Costa Rica came back to lead 2-1 courtesy of goals from Yeltsin Tejeda on 58 minutes and Juan Pablo Vargas on 70 minutes.
Substitute Kai Havertz’s double on 73 minutes and 85 minutes as well as Niclas Fullkrug’s 89th minute effort set up a thrilling finale in the group.
At that stage - as the clock ticked down - a goal for Spain to level 2-2 with Japan would have helped Germany into the Round of 16 on goals scored, the second criteria taken into account after goal difference.
But Luis Enrique’s men couldn’t turn it around in the Khalifa International Stadium, meaning Germany’s comeback 4-2 win proved academic as they were eliminated before the knockout stage for the second World Cup running, following their early departure in 2018, too.