Needing a victory to have any chance of reaching the round of 16, Germany secured its first win of the tournament but bows out at the group stage for a second successive World Cup.
Serge Gnabry gave Germany 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 its 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.