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. The European champions, 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 the 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 the contest, 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 the Spain keeper Iker Casillas from 25 yards with the Real Madrid star getting down well to parry. 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 the 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 the home goalkeeper Itumeleng Khune shortly after the restart. Khune also distinguished himself to keep out an effort from a largely 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, briefly, sparked 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. 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.