XABI Alonso's free-kick in extra-time enabled Spain to see off the challenge of South Africa and clinch third place in the Confederations Cup.
European champions Spain, whose 35-match unbeaten run came to a shock end against the United States in
the semi-finals, were lethargic for much of the encounter and appeared to be heading for another defeat when Katlego Mphela put Bafana Bafana ahead after 73 minutes.
But two goals in the final three minutes from substitute Daniel Guiza turned the match around only for Mphela to send the game into extra-time with a blistering free-kick.
Spain sealed it when Liverpool midfielder Alonso curled a 107th-minute free-kick from the left into the area which eluded everyone and ended up in the bottom right-hand corner.
Spain started the game brightly with David Villa shooting over the bar from close range in the seventh minute.
But South Africa gradually came into it, Siphiwe Tshabalala firing a 20-yard effort wide and Matthew Booth nodding off target after getting on the end of a Tshabalala free-kick.
The impressive Tshabalala then tested Spain keeper Iker Casillas from 25 yards with the Real Madrid star getting down well to parry. Both Booth and Tshabalala both came close shortly after the half-hour mark as South Africa remained on the front foot.
Spain were playing primarily on the counter-attack but it almost paid off with Liverpool winger Albert Riera shooting wide from six yards after 36 minutes.
Vicente Del Bosque's side moved up a gear around half-time with Sergio Busquets heading wide before the break and Villa seeing a 25-yard drive well saved by home goalkeeper Itumeleng Khune shortly after the restart.
Khune also distinguished himself to keep out an effort from a hitherto anonymous Fernando Torres after 51 minutes. Indeed, Del Bosque hauled off first-choice attacking duo Villa and Torres five minutes later, replacing them with David Silva and Guiza.
And the changes did, briefly, spark Spain into life with Riera shooting at Khune and Santi Cazorla firing in a blistering 20-yard effort which the keeper opted to punch over the bar.
But just as Spain were starting to look dangerous, South Africa scored.
Some neat build-up play resulted in Tshabalala escaping down the left and his cross was controlled by Mphela before he lashed the ball high into the net.
Spain rarely threatened an equaliser until Guiza came to the fore late on. His first goal came when he turned in a Cazorla cross with his right foot, finding the bottom left-hand corner with an excellent finish.
His second goal had an element of luck about it with what appeared to be a cross to the far post clipping the upright and going in.
That looked as though it would be enough for Spain but, with the final kick of the match, Mphela lashed in a spectacular 25-yard free-kick which flew past Casillas. Before Sunday, the 24-year-old Mphela had played only 57 minutes in the whole competition.
Both sides had chances to win it in the first period of extra time, Joan Capdevila's cross clipping the bar while at the other end Casillas saved well from Mphela – denying the striker a hat-trick when he had burst through – and Bernard Parker. But it was Alonso who had the final say two minutes into the second period with a clever free-kick which finally ended the hosts' resistance.
"As far as we are concerned we had a good Confederations Cup campaign. It's a positive experience and we are going to try and prepare ourselves better for the World Cup," said Bafana Bafana's Brazilian coach Joel Santana.
"Once we overcame the initial nervousness, the performances of the team improved as we went along. We consider ourselves as a team among equals, including against a team ranked No. 1 by FIFA. Sure we can improve in the future and have a good World Cup.
"Mphela played exceptionally well, he's a very good centre forward who can finish the game really well and can take long shots as well," Santana added.
"He came into the game at a difficult time of the match and scored two goals. His participation for Bafana Bafana is very important and I think he will be the centre forward at the World Cup."
Spain: Casillas, Arbeloa, Albiol, Pique, Capdevila, Santi Cazorla, Alonso, Riera, Busquets (Llorente 81), Villa (Silva 57), Torres (Guiza 57). Subs not used: Diego Lopez, Marchena, Puyol, Pablo, Xavi, Fabregas, Sergio Ramos, Mata, Reina. Booked: Busquets, Pique, Albiol, Llorente.
South Africa: Khune, Gaxa, Booth, Mokoena, Masilela, Pienaar (Mphela 64), Dikgacoi, Sibaya, Modise (Van Heerden 69), Parker, Tshabalala (Mhlongo 84). Subs Not Used: Fernandez, Davids, Mdledle, Fanteni, Moon, Khumalo, Mashego, Gould, Baloyi. Booked: Pienaar, Mphela, Masilela.
Attendance: 21,500.
Referee: M Breeze (Australia).