Substitute Gabriel Martinelli scored deep in injury time as Brazil beat a stubborn Japan 2-1 on Monday to roll into the World Cup last 16.
Arsenal winger Martinelli struck in the 95th minute in Houston to break Japanese hearts in the most dramatic fashion.
The five-time champions will face either the Ivory Coast or Norway in New Jersey on Sunday after edging a captivating knockout encounter.
Japan midfielder Kaishu Sano scored a classy solo goal after 29 minutes following a Brazil mistake to stun the huge numbers of fans decked out in yellow.
Carlo Ancelotti's men hit back 11 minutes after half-time with a header from veteran Casemiro as Brazil dominated the second period.
Then up popped Martinelli right at the death to settle it and send the Brazil fans wild.
History was against Japan: they had never won a knockout game at the World Cup while Brazil were record five-time champions.
But this is not a vintage Brazil side, even with Ancelotti at the helm, while Japan have been talked of as dark horses.
With the Brazil fans' drums providing a constant rhythmic backdrop, Ancelotti's side were on top early on in front of a crowd of 68,777,
Images of Neymar on the bench popped up on the big screens, the noise from the Brazil fans going up another notch.
Defensive midfielder Sano was booked for crunching in on Vinicius Junior, then Brazilian attacker Matheus Cunha forced goalkeeper Zion Suzuki to turn the ball wide.
Japan, who beat Brazil 3-2 in October in a home friendly for their first victory over the South Americans, then enjoyed a spell in the ascendancy.
Just before the half-hour mark Hajime Moriyasu's team took the lead, and it stemmed from an error by Danilo as he gave the ball away with a sloppy pass.
Sano, one of four changes from Japan's 1-1 draw with Sweden at the end of the group phase, picked the ball up in the centre circle and got away from the 34-year-old Casemiro -- who was on a booking -- with ease.
Sano, who plays for Mainz in Germany, drove upfield before sweeping the ball past goalkeeper Alisson. It was his first goal for his country.
Now the Brazil supporters were edgy, groaning when a move broke down or a pass went backwards.
Brazil, whose last World Cup triumph was in 2002, looked unconvincing at the back and ineffective in attack.
Ancelotti was forced into a change at the break, an emotional Lucas Paqueta limping off in distress with the attacking midfielder replaced by 19-year-old striker Endrick.
Japan, who thrashed Tunisia and held the Netherlands and Sweden to reach the last 32, had Brazil where they wanted them.
Brazil ramped up the pressure to start the second half and Japan defender Takehiro Tomiyasu twice kept the ball off the line, not that he knew much about it.
Brazil were soon level, Arsenal defender Gabriel sending in an inviting cross for midfielder Casemiro to head in unmarked at the back post.
The danger man Vinicius, well stifled up until then, turned the Japan defence inside out and stabbed the ball towards Suzuki, who directed it onto the post.
It was all Brazil as they pushed for a late winner, with Vinicius -- who scored four times in the group stage -- increasingly in the thick of it.
Then came Martinelli's late intervention.