Xabi Alonso scored on a free kick in extra time 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, allowing the European champions to rally from their first loss in 35 games, a 2-0 defeat in the semifinals to the United States.
"It's a nice way to forget about the loss to the U.S.," Spain coach Vicente del Bosque said. "The match was difficult for us. Our opponent created a lot of difficulties for us."
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.
"We gave our all. We made it very difficult for them but I'm very, very devastated," South Africa captain Aaron Mokoena said. "The boys deserve credit."
With the weight of expectations lifted after reaching the semifinals, Bafana Bafana played well and Mphela had South Africa poised to squeak out its second win of the tournament when he scored his first goal in the 73rd minute. Breezing into the penalty area, he controlled Siphiwe Tshabalala's cross with his knee and shot over goalkeeper Iker Casillas for a 1-0 lead.
The game remained that way until the 88th minute, when Guiza chested down David Silva's long pass and sent a low shot beyond Khune to even the score. Moments later, Guiza lofted a chip from the right edge of the area that floated over Khune and bounced into the goal off the far post.
"He said he shot it but I'm not so sure," Alonso said, jokingly. "It was a key score."
Some of the crowd began filing out of the stadium as the game went to extra time, and they missed Mphela's tying shot three minutes later. The goal was all the more surprising considering he'd played only 57 minutes in the tournament.
South Africa couldn't convert on chances to win the game in extra time.
Casillas saved a shot from Mphela in the 98th minute and denied Bernard Parker soon after as Bafana Bafana finished the World Cup warm-up tournament with three straight losses.
"If you switch off against teams like this you get punished, and that's what happened," Mokoena said. "We could have done better."
Del Bosque had fielded a near full-strength lineup with David Villa and Fernando Torres both trying to finish the tournament as top scorer.
The pair, who both had three goals, guided Spain's attack early, with Villa cutting back along the left of the area in the fifth minute to unleash a curling shot that Khune tipped wide.
Screams of "Boo" came 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 minute before Casillas dived to tip Tshabalala's half-volley wide in the 32nd.
"It was a match of equals and the team could have won. Spain was lucky," South Africa coach Joel Santana said. "Bafana Bafana played very good, gave two goals against Spain. They played very well against a team that is best in rankings."