Stoppage-time goals from Philippe Coutinho and Neymar earned 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.
Coutinho, the scorer of a superb goal against Switzerland, almost opened the scoring early on when picked up possession after 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 lone forward Marco Urena.
Cristian Gamboa blasted well over from distance before Brazil came forward and Willian was fouled to give Brazil a free kick close to the Costa Rica area, but Neymar’s delivery 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 from 12 yards by Celso Borges.
Another Neymar free kick was headed away by Giancarlo Gonzalez, with Brazil’s early fizz beginning to fade, before Coutinho overhit a pass towards Gabriel Jesus and Silva headed another Neymar delivery 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 from outside the area 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.
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, curling 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 with an opening goal 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 around 15 yards out but his shot cannoned off a defender.
Costa Rica then made their first switch, replacing Urena with Christian Bolanos before, after 57 minutes, Brazil were foiled again when Navas tipped over Neymar’s close-range effort.
Coutinho had another effort stopped by Navas after a deft touch by Jesus had given him the chance to shoot before Neymar broke but could not find the right pass for the supporting Costa.
With the game heading into its 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 offer 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 tto the edge of the area but curled over.
Costa Rica took over Gamboa and replaced him with another defender, Francisco Calvo — and with 12 minutes to go Brazil thought they had a penalty when referee Bjorn Kuipers adjudged that Neymar had been pulled back inside the area by Gonzalez.
However, the official overturned his decision after a VAR review, wiping out the penalty but opting against booking 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 with yet another Brazil attempt from outside the box.
But Coutinho finally made the breakthrough, racing in and keeping his cool from close range after Firmino’s header and a Jusus touch helped the ball on, before Neymar turned Costa’s cross into an empty net.