Brazil squeezed through to the quarter-finals of the World Cup after coming within a whisker of going out against an accomplished Chile side.
Neymar's penalty in the shootout proved decisive when Chile's ex-Nottingham Forest, West Brom and Brighton defender Gonzalo Jara smashed the final spot-kick against the post.
The win - 1-1 after extra time and 3-2 on penalties - keeps Brazil's dreams alive of winning a World Cup on home soil - and their record of not losing a competitive match at home since 1975.
Chile had taken Brazil all the way in Belo Horizonte, dominating for long spells of the second half and nearly snatching it at the death through Mauricio Pinilla's strike which struck the crossbar.
Brazil had taken the lead through a goal awarded to David Luiz - even though it appeared to come off a Chile player - before Chile netted a deserved equaliser through Alexis Sanchez.
Brazil's win also saved English referee Howard Webb from becoming the country's public enemy number one after he found himself at the centre of huge controversy when he disallowed a goal by Brazil forward Hulk for handball.
If Brazil had enjoyed the rub of the green from officials in previous matches - something that had been flagged up by Chile - there was no danger of such an accusation being made against Webb.
He had also turned down a penalty claim by Hulk, as well as one by Chile's Eduardo Vargas, while Brazil coach Luiz Felipe Scolari was furious he did not take a tougher line on some forceful challenges on Neymar.
It was Neymar who was the man at the centre of most of the action for Brazil in the first half but it was Marcelo who shone first with a skilful turn on the edge of the box that fooled his marker and earned the Real Madrid full-back space to send a low shot just a yard past the post.
Chile looked threatening on the break but Brazil dominated possession and took the lead in the 18th minute - claimed by Luiz though probably an own goal.
Neymar's corner was headed on by Thiago Silva and it initially looked as though former Chelsea defender Luiz finished at the far post but replays appeared to show it went in off Jara.
That should have been the signal for Brazil to assume total control but instead Chile pounced on a shoddy mistake to equalise.
Hulk messed up a return to Marcelo's throw in by their own touchline, Vargas snapped up the ball and found Sanchez who neatly slotted home his second goal of the tournament.
Neymar continued to look Brazil's biggest hope and his header from Oscar's cross just bounced off a defender's arm which diverted the angle just enough to take it past the post.
Oscar found his range and his brilliant cross-field pass found Neymar again threatening but he dallied, Chile scrambled the ball clear but it ricocheted off Fred and just over the bar.
A dipping strike from distance by Dani Alves was turned over by Claudio Bravo, before Brazil's out-of-form striker Fred made perhaps the most embarrassing miss of the World Cup so far, miss-hitting the ball hopelessly just before the break.
But Brazil always looked vulnerable and another error, by Luiz Gustavo this time, saw Sanchez threaten again but Luiz managed to hurl himself in to block for a corner.
The second half saw Brazil frustrated by nerves and by Webb, while Scolari's decision to push Neymar further forward saw him drop out of the game.
Then came the controversy. Brazil believed they had gone in front after Marcelo's long ball was brought down by Hulk, who then sliced it into the bottom corner, but it was wiped off by an assistant's flag who said the striker controlled it with upper arm. It was a controversial decision and to rub salt in the wound Webb booked him.
Chile then began to assume an air of superiority while Brazil looked worried, and were grateful to wonderful save by Julio Cesar to turn aside what looked a certain goal for the excellent Charles Aranguiz.
Former Manchester City striker Jo came on for Fred and looked destined to score straight away but somehow missed, then Neymar's flicked header was straight at Bravo.
The Chile keeper stood up strong again after brilliant work by Hulk, but it was Brazil who were relieved to take the game to extra time.
Oscar had a half-chance with a header, Bravo kept out Hulk but it was Chile who nearly snatched it at the death but Pinilla's blistering strike crashed back off the crossbar.