Substitute Daniel Alves broke South African hearts with an 87th-minute free-kick for Brazil that dumped the hosts out of the Confederations Cup with a narrow semi-final victory in Johannesburg.
The Barcelona right-back, who only came on for the closing stages of the game, smashed in a powerful 18-yard set-piece from the left to stun the huge crowd at the Ellis Park Stadium after their side had performed impressively for most parts of the game.
However, they eventually fell short and will now meet Spain in the third-place play-off on Sunday, while the Selecao will face the United States in the tournament finale.
Bafana Bafana, who can take heart from the display, were handed a massive boost before the start after striker Bernard Parker passed a late fitness test and they looked inspired during the early stages with Steven Pienaar and Teko Modise bossing the midfield.
However, it was Luis Fabiano that created the first opening after 13 minutes when he fired a low shot from inside the box that forced Itumeleng Khune to dive low to his left.
The hosts responded with Siboniso Gaxa beating Andre Santos to a 50-50 ball on the right and then unleashing a 20-yard cross-goal shot that flew agonisingly across the face of goal.
They continued to hold their own with Aaron Mokoena then missing a gilt-edge chance in the 21st minute - the South Africa captain received a beautifully floated free-kick from Modise on the right, but put the close-range header over.
Just before the half-hour mark, Parker was clumsily brought down by Luisao 25-yards out and Siphiwe Tshabalala came closest to breaking the deadlock with a well-hit free-kick that needed to be palmed over by Julio Cesar.
Andre Santos then shot straight at Khune with a long-range effort after bringing the ball down nicely, with the South Americans largely restricted to shooting from distance.
Kaka followed that up by drifting infield from the left and curling narrowly wide, while at the other end, Pienaar finally decided to shoot and superbly he did - Julio Cesar getting the faintest of touches to a curling shot from outside of the box that ended up wide.
After the restart Robinho fired another wayward shot from distance, but again the tempo of Joel Santana's team posed plenty of concern for the visitors with Modise's deflected shot off Luisao forcing Julio Cesar to scramble to his left and tip the ball around the post.
When the Manchester City striker did manage some space in the box, he fired wide, with Luis Fabiano's shot also following a similar path.
For the next 15 minutes, all the play was in midfield, with Gaxa and Modise firing over for the home side.
With nine minutes remaining, Dunga looked to change things by introducing Daniel Alves for Andre Santos and for whatever reason he made the change, it seemed to work.
Then, as time ran out, Mokoena brought down Ramires almost exactly on the 18-yard line and the Barcelona right-back smashed the resultant free-kick, which took a slight deflection off Pienaar, into the top corner leaving the almost 50,000 Ellis Park crowd massively disappointed.