In the 96th minute, the world's most expensive player, Neymar, netted his first goal of the 2018 World Cup and his side's second to secure a priceless result and ease pressure going into their final game.
Barcelona's Coutinho, who scored a superb goal against Switzerland, nearly opened the scoring early on when Bryan Ruiz gave the ball away, but thumped his shot high.
Having been frustrated for nearly the entire game, Coutinho poked the ball past Costa Rican goalkeeper Keylor Navas one minute into stoppage time before Neymar's volley wrapped up the points for a relieved Brazil.
Brazil leave it late to beat Costa Rica in return to winning World Cup form.
Neymar has been offered support by his coach Tite after the Brazil star was seen crying at the end of a dramatic victory over Costa Rica.
Only a fingertip over the crossbar denied Neymar when he met Douglas Costa's cutback sweetly on the volley on 55 minutes, and the goalkeeper stood tall again when Coutinho ran onto a layoff from Jesus. The seleção saw chances from Gabriel Jesus, Coutinho, and several shots from Neymar, who just missed breaking the game open on a multitude of occasions.
Saudi Arabia wins emerging-market status in MSCI upgrade
MSCI's emerging equity benchmark index weakened 0.8 percent, having now chalked up losses for six of the past seven sessions. Separately, MSCI said it would upgrade Argentina to the category as well.
How to Watch Brazil vs Costa Rica
Unlike Argentina and Lionel Messi, Brazil is still in a good position to advance to the round of 16. Serbia now leads Group E with three points while Brazil and Switzerland are tied at one.
Can Argentina qualify for the World Cup knockout stages?
The win ensured Nigeria's qualification to the knockout round goes down to the wire - when they play Argentina on Tuesday. Nigeria defeated Iceland 2-0 at the Volgograd Arena on Friday to keep their chances of progressing from Group D alive.
After frustrating the Brazilians for 90 minutes, the Costa Ricans finally conceded twice in stoppage time and slumped to a second loss that ended their chances of advancing to the knockout stage.
The score ended 2-0 in Friday's Group E fixture at the Krestovsky Stadium, Saint Petersburg.
With the game still scoreless 13 minutes from time, Neymar collapsed under a soft challenge in the box and thought he had won a penalty for his misfiring side. Costa Rica were eliminated from the tournament. Costa Rica has been eliminated.
But that was not to be because centre referee Björn Kuipers after reviewing the incident using Video Assisted Referee (VAR), reverted his penalty decision.
That was an apt reflection of how tense the match had been, with Costa Rica's flat-back five keeping Neymar on the periphery, as Johan Venegas and Cristian Gamboa man-marked him out of the game.