It was a game full of drama - VAR decisions and missed chances for Brazil made it look like Costa Rica were going to gain a historic point against the five-time champions, but it wasn't to be.
Goals in injury time from Philippe Coutinho and Neymar were enough to seal the victory, as the favourites to win the tournament celebrated in samba style at the expense of Costa Rica, who are eliminated.
Frustrating first half for Brazilians
It took Brazil a while to get going in St Petersburg, with the best chance of the half falling early on for Celso Borges of Costa Rica.
The Deportivo La Coruña midfielder found himself in space on the edge of the box with the ball rolling towards him at pace, but he failed to direct his effort goalless. Brazil fans in the stadium breathed a collective sigh of relief.
After about twenty minutes of sparring and no chances, Brazil stepped up the pressure.
Gabriel Jesus saw his goal ruled out for offside, after a brilliant turn and finish, before Coutinho was denied by Keylor Navas with a spectacular save.
Brazil created more and more chances in the opening period, but failed to break down a resilient Costa Rica defence.
The right hand side was completely nullified, with Chelsea man Willian failing to affect the game and third choice right-back Fagner starting ahead of the injured Danilo.
Brazil step it up but struggle to find breakthrough
Tite responded to the lack of threat on the right by sending on Juventus's tricky winger Douglas Costa. The skilful nature of his game was helpful for the Brazilians, who were now less predictable.
Gabriel Jesus looked the more likely to score and came agonisingly close early on the second half as his effort rattled the crossbar after he hung in the air and headed towards goal.
Neymar came close on a number of occasions, but the Real Madrid goalkeeper Navas answered all questions thrown his way, making some excellent saves.
It seemed it was going to be a frustrating afternoon for the Paris Saint-Germain star-man Neymar, who had the best chance of the game as he went through on Navas's goal, ultimately firing wide of the mark from close range.
His match got even more frustrating with ten minutes to go, when the referee Björn Kuipers dramatically overturned his own penalty decision.
Neymar was tugged at in the box, but made a meal of it and used theatrics to throw himself to the ground. The referee consulted VAR and changed his mind.
Costa Rica were given a lifeline through VAR. It seemed they were going to hold on for a point against a very star-studded Brazil, who brought on the likes of Roberto Firmino.
Costa Rica's resistance finally broken
However, the goal finally came for Tite's Brazil, in the first minute of additional time. A mixture of impressive touches from Firmino and good strength from Jesus led the ball into the path of Coutinho who poked the ball home for his second goal of the tournament.
Minutes later, Brazil added another goal as Neymar tapped home from short distance.
A valiant effort by Costa Rica, especially their goalkeeper Navas, was ultimately punished as the Central American side are now eliminated from the tournament, regardless of the result in the later game in the group.
For Brazil, they gained a victory which was deserved, but one they had to throw everything at. Next up for the favourites is a game against Serbia in Moscow, in a game where a victory will send them to the knockout rounds.