South Africa (13) 39
Tries: Fourie, Steenkamp, De VilliersCons: Morne Steyn 3 Pens: Morne Steyn 6
Australia (31) 41
Tries: Beale, O'Connor, Moore, Elsom, Mitchell Cons: Giteau 4, O'Connor Pens: Giteau, Beale
Wallabies captain Rocky Elsom scores his side's fourth try
Australia edged a classic Tri-Nations encounter in Bloemfontein, with a last-gasp penalty from Kurtley Beale sealing their win over South Africa.
The Wallabies led 31-13 at the break, but the hosts came storming back and grabbed the lead with 20 minutes left.
A Drew Mitchell try and James O'Connor conversion made it 38-36 to Australia before Morne Steyn slotted a penalty to make it 39-38 to the Springboks.
But Beale was on target from 56m to condemn the hosts to the wooden spoon.
Beale's late penalty helped Australia end a 47-year drought at South African altitude and ensured they finished second in the Tri-Nations table behind champions New Zealand.
"It was a great game of rugby and good for the code," Australia coach Robbie Deans said. "In the end we are just glad to have got home.
"I was pretty positive when KB [Kurtley Beale] got the ball at the end there. He's a positive guy."
South Africa finished the campaign with only one win from six matches, while Australia head into their final match against the All Blacks in Sydney next weekend with two wins from five.
Last Saturday, South Africa twice came back from a 14-point deficit to win 44-31 and at one stage it looked like history was going to repeat itself at Vodacom Park.
Australia were gifted an early three points when the out-of-form Bryan Habana was punished for not releasing and Matt Giteau slotted the penalty.
Steyn responded for the hosts after Beale's illegal attempt to steal the ball but the Waratahs back made amends almost instantly - Jean de Villiers failed to hold his position, which allowed O'Connor to send his team-mate away for the opening try after eight minutes.
Giteau added the extras, before another Springbok error, this time a rare one at the line-out, led to another try for Australia - John Smit's throw was too long, enabling the Wallabies to steal the ball and spread it quickly to Ashley-Cooper, whose pass sent O'Connor over.
Giteau added the conversion and after Steyn put over another penalty, hooker Stephen Moore crossed for his third Test try, set up by Mitchell's break through the middle.
Australia added a bonus point-try after 24 minutes but again had Habana to thank after the winger, who was roundly booed thereafter, missed his tackle on O'Connor, who sliced through the centre and off-loaded to skipper Rocky Elsom to score.
Giteau extended the lead to 31-6 before a moment of genius from Springboks lock Victor Matfield, who chipped the ball over the last defender before regathering and laying the ball on for Jaque Fourie to score, silenced the jeers. Steyn converted to leave Peter de Villiers' side trailing by 18 at the break.
Prop Gurthro Steenkamp cut that gap by five points six minutes after the restart when he powered his way over and the conversion, along with a penalty from Steyn, further boosted the visibly rejuvenated hosts.
The upward swing in momentum gathered even more pace after 54 minutes when centre Jean de Villiers' cut right through the Australia defence and crossed under the posts.
The comeback was complete on the hour mark when a 50m penalty from Steyn put the home side ahead for the first time.
Australia's woes deepened 10 minutes later when replacement hooker Saia Faingaa was sent to the sin bin after a spear tackle on Flip van der Merwe and Steyn extended the lead to five.
But Mitchell crossed under the posts with eight minutes to play and O'Connor reclaimed the lead on behalf of the tourists with a simple conversion.
An error from Beale four minutes from time presented Steyn with his ninth opportunity at goal from the right touchline and he maintained his 100% record to put his side back ahead.
However, Beale had the final say, finding the target from the halfway line and ensuring the visitors finished the Tri-Nations above the hosts and also in possession of the Nelson Mandela Shield.
"It's definitely a highlight of my career," Wallabies matchwinner Beale said.
"At first I didn't know I was going to take the kick. I was looking for Gits [Matt Giteau]. But I just had to block out the crowd, and two seconds after I kicked it I knew it was going over."
Australia coach Robbie Deans said: "This was the most experienced Bok side in history up against a young Wallabies side, so to achieve that landmark (a win on the Highveld) is significant.
"It will serve us well going forward."