Philippe Coutinho and Neymar bailed Brazil out with stoppage-time strikes against Costa Rica following another uninspiring display at the 2018 World Cup.
Coach Tite's team were heading for a second inglorious draw when Barcelona midfielder Coutinho popped up to poke home from Roberto Firmino's nod down.
And with seconds remaining Neymar, the world's most expensive player, tapped in Douglas Costa's cross to secure Brazil's first win of the tournament.
Their hopes of victory looked to have been dashed when they had a penalty decision overturned by VAR in the 78th minute after referee Bjorn Kuipers had initially ruled Neymar had been brought down in the area.
As it stands Brazil currently lead Group E and could go through with a draw in their final pool match against Serbia.
Philippe Coutinho's stoppage-time winner saves Brazil
How this Brazil side could do with just a sprinkling of the brio of the teams of 1970 and 1982.
In the two games they have played so far the class of 2018 have been pedestrian for long periods - reminiscent of Carlos Alberto Parreira's heavily-criticised champions from 1994.
Even that side scored more goals in their opening two games than Tite's outfit, who too often are cautious in their approach play and whose illustrious attack have yet to replicate their club form.
Brazil were listless in the opening 45 minutes.
After being fouled 10 times in the opening game against Switzerland, Neymar, perhaps unfairly criticised after that match for not being a team player, was once again targeted by defenders and spent more yet time with his face in the turf.
Barcelona's Coutinho, who scored with a brilliant strike in the opening game, failed to find his range and Chelsea's Willian, taken off at half-time, was awful on the right.
The tempo increased, as did the chances for Brazil, after the break, although prior to the goals Costa Rica keeper Keylor Navas was only tested by Neymar's snapshot, while Jesus' header came off the woodwork.
With 15 minutes remaining it was desperation time for the five-time winners, with Tite and the vast number of Brazilians inside the Saint Petersburg stadium hoping for a miracle.
Those prayers appeared to be answered with 12 minutes remaining, as official Kuipers pointed to the spot after he judged Giancarlo Gonzalez had pulled down Neymar.
However, after watching the replay the experienced Dutchman overturned the decision - and Brazilian shoulders slumped.
Then came the breakthrough, with the fourth official signalling six minutes of stoppage time.
With one minute of it played, Coutinho broke the deadlock when he fired in after substitute Firmino and Jesus had combined.
And in the dying seconds, Neymar, only recently recovered from a broken foot, tucked home Juventus midfielder Costa's square ball.
Brazil's talisman wept after the final whistle - the win relief both for him and for his nation.
Teammate and man-of-the-match, Coutinho, said the 26-year-old's "joy at being back on the pitch is contagious".
"We know that Neymar had a difficult injury and he went through a difficult patch, Everybody is so happy for him being back," said the Barcelona player.
The emotions of the dramatic win caught up with Brazil forward Neymar after the final whistle
It was a commendable defensive display by Costa Rica, who had hoped to punish Brazil on the counter-attack.
However, their only opportunity came in early in the first half when Celso Borges fired wide after a good run and cross from Celtic's Cristian Gamboa.
There will be no repeat of 2014, when Los Ticos reached the last eight.
Brazil coach Tite, who tripped over as he ran on the field to celebrate Coutinho's opener: "I think I kind of pulled a muscle.
"I'm limping now after the celebration. We were a bit over-excited.
"The first half was difficult but in the second half, we couldn't have done any more. If we have suffered in these first two games, it is simply because this is a World Cup.
"If you look at the games yesterday, and the favourites so far, they are either losing or they are finding it tough and maybe winning 1-0."
Brazil goalscorer Coutinho: "It was a tough game as we expected. We showed good patience to play until the last minute where we were rewarded with those two goals.
"We know the responsibility is huge when you're playing for Brazil. Emotionally, the professor (Tite) always highlights the mental strength we need from the start to the end."
Costa Rica coach Oscar Ramirez on his defensive tactics: "I don't know what else we could have done.
"What we did I think was perfect. You have to think we were playing against the second best team in the world. They have great players.
"Considering what I have at my disposal and what they have, I think it is reasonable what we did."
There were not many standout performers for Brazil, but having scored the opening goal and managed four efforts on target, the nod goes to the former Liverpool man
Brazil face Serbia in their final game next Wednesday (19:00 BST) in Moscow, and Costa Rica end their campaign against Switzerland (19:00 BST) in Nizhny Novgorod on the same day.