Dramatic late strikes by Philippe Coutinho and Neymar gave Brazil their first win of the World Cup against Costa Rica and broke Central American hearts by sending them out of the tournament.
The result on Saturday (NZ time) leaves the five-times world champions with four points from two games after they drew 1-1 with Switzerland in their opening match.
The clock had ticked past 90 minutes when Marcelo crossed from the left and substitute Roberto Firmino headed the ball back across goal to Gabriel Jesus.
Jesus's first touch took the ball away from the defender and Coutinho raced in to poke past Keylor Navas from six metres out and claim his second goal of the tournament.
Neymar added the second with practically the last kick of the game, racing on to Jesus's square ball following a quick counter-attack as Costa Rica were forced to push for the equaliser.
The forward, who thought he had won a penalty in the 77th minute only for the referee to reverse the decision after consultation with the video assistant referee (VAR), was overcome by emotion on the whistle, covering his face as tears streamed down his cheeks.
For long periods Costa Rica's flat-back five kept Neymar on the periphery, with Johan Venegas and Cristian Gamboa both man-marking him out of the game as Brazil found it hard to carve out chances in the first half.
Marcelo's tame shot straight at Navas in the 41st minute was the first effort on target from either side before the break, and the team went into halftime with the boos and whistles of their own fans ringing in their ears.
Celso Borges spurned arguably the best chance of the first period for Costa Rica when he ran on to Gamboa's cut back but shot wide from eight metres out.
The result means Brazil have avoided the ignominy of equalling their longest winless run at the World Cup.
Meanwhile, Nigeria have done themselves, and struggling two-time champions Argentina, a favour with a 2-0 victory over Iceland.
Nigerian striker Ahmed Musa dramatically marked his return to the team with two fine second-half goals, with the result leaving both sides, and struggling Argentina, in the hunt to go through from Group D along with already-qualified Croatia.
But Argentina and World Cup debutants Iceland have to win their respective games against Nigeria and Croatia next week to have a chance of advancing.
In only the second win for an African side in Russia so far, Nigeria broke the deadlock in the 49th minute when Victor Moses, playing as a wing-back, lofted a pass towards Musa who deftly controlled the ball and smashed in the half volley.
Musa, recalled to the starting team by coach Gernot Rohr, made it 2-0 in the 75th minute when he found space on the left, beat Iceland defender Kari Arnason and rounded goalkeeper Hannes Por Halldorsson before slamming the ball into the net.
Iceland, roared on by thousands of fans from the tiny country, kept pushing and were awarded a penalty in the 80th minute when referee Matt Conger used the Video Assisted Referee system to confirm that striker Alfred Finnbogason had been fouled by Tyronne Ebuehi.
But Iceland's playmaker Gylfi Sigurdsson blasted his effort high over the bar.