Needing a victory to have any chance of reaching the round of 16, Germany secured their first win of the tournament but bow out at the group stage for a second successive World Cup.
Serge Gnabry gave them a richly deserved early lead, but Yeltsin Tejeda equalised with his first international goal in the second half and Costa Rica were heading for the knockout stage after Juan Pablo Vargas put them in front.
Havertz levelled not long after Vargas' goal and struck again to put Germany back in front, with fellow substitute Niclas Fullkrug then adding a late fourth goal
It was Japan and Spain who advance from Group E, though, with the Samurai Blue finishing top after their surprise 2-1 win over Luis Enrique's side.
Gnabry opened the scoring only 10 minutes in, meeting David Raum's cross from the left with a glancing header that found the far corner of the net.
Leon Goretzka's header was kept out by Keylor Navas, Musiala fired wide after turning sharply and surging into the box and Gnabry was only just off target with a right-foot drive as Germany continued to dominate.
But Keysher Fuller almost equalised from out of the blue late in the first half, Manuel Neuer superbly tipping his shot around the post after a mistake from Antonio Rudiger.
News of Japan leading Spain left Germany fans with hands on heads and things took another turn for the worse when Tejeda was on hand to finish from close range after Neuer could only palm Kendall Waston's header into his path in the 58th minute.
Musiala twice struck the right-hand post and Rudiger hit the outside of the same upright as Germany desperately tried to stay alive.
Costa Rica then stunned Germany once again by taking the lead after 70 minutes, Vargas getting the final touch when Neuer as Germany failed to deal with a ball into the box.
Los Ticos' joy was short-lived, though, as Havertz finished clinically with his right foot soon after coming off the bench just a few minutes after Germany had fallen behind.
Navas denied Fullkrug with a magnificent save from close range, but Havertz tucked in coolly with his left foot at the back post.
Fullkrug looked to have strayed offside, but was awarded a second goal in as many games in the closing stages following a VAR check after he slotted in from close range, but Germany bowed out on goal difference.
What does it mean? Another dark day for Germany
Germany left it too late to keep their World Cup hopes alive, paying the price for a surprise loss to Japan in their first game of the tournament.
They finished third, level on points with Spain but falling short after Japan caused another upset to take top spot, with Costa Rica bottom.
After crashing out at the same stage in Russia four years ago, there will be another inquest following Hansi Flick's first major tournament at the helm.
Muller misfires again
Thomas Muller started in an advanced role once again and he was unable to make the desire impact.
Flick will surely be questioned about opting against starting either Havertz or Fullkrug, who also scored in the 1-1 draw with Spain. Muller has failed to score in seven World Cup appearances, having scored 10 goals in his first 12 games in the competition and the 33-year-old has surely played his last World Cup match.
Musiala was the best player on the pitch and will have a massive part to play in Germany's rebuild.
He could easily have had a hat-trick and caused Costa Rica all sorts of problems in an influential display.
Key Opta facts
Germany have been eliminated from the group stages at consecutive World Cups, having progressed from the opening stage in each of their 16 previous appearances at the tournament.
Germany have failed to keep a clean sheet in 12 matches at major tournaments, further extending their longest such streak.
Costa Rica named their oldest average starting line-up for a World Cup match (31 years and 37 days), with eight of their starters aged 30 or over.
Havertz became only the second Germany player to score a brace as a substitute in a World Cup match after Andre Schurrle versus Brazil in 2014.
Neuer played his 19th World Cup match, becoming the goalkeeper with the outright most appearances in the history of the competition.
While Germany and Costa Rica are packing their bags, Japan will play Croatia on Monday and Spain take on Morocco 24 hours later.