Stoppage-time goals from Philippe Coutinho and Neymar gave Brazil a 2-0 win over Costa Rica after they had seen a late penalty award overturned by VAR.
Coutinho raced onto a touch from Gabriel Jesus and prodded the ball past Keylor Navas, with Neymar firing in from close range in the final seconds as Costa Rica went out of the World Cup.
Barcelona's Coutinho, who scored a superb goal against Switzerland, almost opened the scoring early on when Bryan Ruiz gave the ball away, but thumped his shot high.
Brazil were camped in the Costa Rica half, but Thiago Silva had to be alert at the other end when Ruiz threaded a dangerous ball towards Marco Urena.
Cristian Gamboa hammered well over from distance before Brazil came forward and Willian was fouled but Neymar's delivery into the area was headed clear.
But after 13 minutes, Costa Rica had a great chance to take the lead when Gamboa's pull-back from the right was steered wide by Celso Borges.
Another Neymar free kick was headed away by Giancarlo Gonzalez, with Brazil's early fizz fading, before Coutinho overhit a pass towards Gabriel Jesus and Silva headed a Neymar cross wide.
Jesus had the ball in the net after 26 minutes only to be denied by the offside flag, and a minute later Brazil could have broken through when Coutinho's pass freed Neymar, whose heavy first touch allowed keeper Keylor Navas to smother.
Coutinho curled over as Tite's side picked up the pace again, and a great piece of skill by Neymar enabled him to clip a cross to Jesus, who was adjudged to have fouled a defender as he jumped.
With Brazil increasingly trying their luck from outside the box as Costa Rica defended with discipline, Willian shot over and Navas saved Marcelo's low strike.
Brazil made a change at the break, bringing on Douglas Costa for Willian, and their bright start to the second period was almost rewarded when Jesus crashed a header against the bar after 49 minutes.
A minute later, another chance came and went as the ball fell to Coutinho 15 yards out but his shot cannoned off a defender.
Costa Rica made their first switch, replacing Urena with Christian Bolanos before, after 57 minutes, Brazil were foiled again when Navas superbly tipped over Neymar's crisp strike.
Coutinho had another effort stopped after a deft touch by Jesus had given him the chance, before Neymar broke but could not find the right pass for the supporting Costa.
With the game heading into the final 20 minutes, Brazil brought Roberto Firmino into the fray for Paulinho, a scramble in their own area shortly beforehand having offered a reminder that Costa Rica could still pose a threat.
A Neymar corner was headed straight at Navas by Silva, and then Neymar won the ball back inside the Costa Rica half and raced to the edge of the area but whipped the shot wide.
Costa Rica took off Gamboa and replaced him with Francisco Calvo -- and then, with 12 minutes to go, Brazil seemed to have a penalty when referee Bjorn Kuipers adjudged that Neymar had been pulled back inside the area by Gonzalez.
However, the official overturned thats decision after a VAR review, wiping out the penalty but opting not to book Neymar for appearing to make the most of the challenge.
Costa Rica brought off Guzman and introduced Yeltsin Tejeda and, as the minutes ticked away, Casemiro shot straight at Navas and Neymar was booked for dissent after throwing the ball to the ground.
But Coutinho finally made the breakthrough, racing in and keeping his cool from close range after Firmino's header and a Jusus touch had helped the ball on, withe Neymar turning Costa's cross into an empty net seconds from the end.