Rustenburg:Xabi Alonso scored from a free kick in extra time on Sunday to give Spain a 3-2 win over South Africa and third place at the Confederations Cup.
The Liverpool midfielder curled his shot through a crowd in the penalty area and inside the far post as the European champions rallied from their first loss in 35 games, a 2-0 defeat in the semifinals against the United States.
"It's a nice way to forget about the loss to the U.S.," Spain coach Vicente del Bosque said.
South Africa substitute Katlego Mphela scored two goals, including an injury-time equalizer, to send the match into extra time after Dani Guiza had scored in the 88th and 89th minutes.
Khune was left flat-footed as Alonso's curling free kick from the left landed inside the far post as Spain rallied from the disappointment of seeing a record 15-game winning streak and 35-game unbeaten run snapped by the United States.
With the weight of expectations lifted after having at least reached the semifinals, Bafana Bafana played well and Mphela sent the match into extra time at the Royal Bafokeng Stadium on the last play of injury time with a free kick from outside the area that sailed past goalkeeper Iker Casillas into the top of goal.
Mphele first scored by breezing into the penalty area, controlling Siphiwe Tshabalala's cross with his right knee and shooting over Casillas with his left foot in the 73rd, nine minutes after replacing Steven Pienaar.
South Africa seemed poised to squeak out its second win of the tournament before Guiza chested down David Silva's long pass into the area and sent a low shot beyond Khune to start the rally. Moments later, Guiza lofted a chip from the right edge of the area that floated over Khune and bounced into the goal off the inside of the far post.
"We gave our all. We made it very difficult for them but I'm very, very devastated. The boys deserve credit," South Africa captain Aaron Mokoena said. "If you switch off against teams like this you get punished and that's what happened. With both goals we could have done better."
Casillas saved a shot from Mphela, who had equalized with a free kick in the third minute of injury time, in the 98th and denied Bernard Parker soon after as Bafana Bafana finished the World Cup warm-up tournament with three straight losses.
Del Bosque had fielded a near full-strength lineup with David Villa and Fernando Torres both looking to finish the World Cup warm-up tournament as top scorer.
The pair, who both finished with three goals, guided Spain's attack from the start, with Villa cutting back along the left of the area in the fifth minute to unleash a curling shot that Khune dived to tip wide.
Screams of "Boo" reigned down from the stands as South Africa defender Matthew Booth saw two early chances from crosses just miss.
South Africa's defense scrambled to deny Villa and Torres chances by the 20th before Casillas dived to tip Tshabalala's half-volley wide in the 32nd, with Booth hitting the rebound wide.
"My defense played very good," South Africa coach Joel Santana said.
Guiza and Silva replaced Villa and Torres in the 57th and Spain improved with Khune saving Riera's half-volley at the near post in the 62nd before punching Santi Cazorla's shot out two minutes later.