Bafana Bafana led until two minutes from normal time through substitute Katlego Mphela, before two goals in a minute from Daniel Guiza looked to have sealed the win for the Spanish.
However, Mphela came up with the goal of the tournament in the dying seconds of injury time as he rifled home a free-kick from 35 yards.
It was all that Bafana deserved after another excellent display against top-quality opposition, but in the end Xabi Alonso’s fortunate goal in the second half of extra time denied them a famous win.
Gaxa had an early overlapping chance, but his ball into the box, a problem all tournament, was weak and Spain were easily able to clear.
Khune was forced into an excellent save from David Villa at the other end as he stretched out an arm to deny the Valencia man’s placed shot that was heading for the far corner.
A sustained period of pressure from Bafana resulted in Siphiwe Tshabalala shooting wide from the edge of the area with his less favoured right foot, while Matthew Booth should have done better at the back post from Tshabalala’s free-kick in a miss reminiscent of captain Aaron Mokoena against Brazil in the semifinals on Thursday night.
A magnificent saving tackle from Tsepo Masilela denied Santi Carzola a shooting chance as Spain broke quickly from a cleared South African corner, while Macbeth Sibaya provided a similar piece of defending against Fernando Torres.
Parker went on a mazy run that had the Spanish defence at sixes and sevens, but when the time came to pull the trigger, he appeared to lose track of the ball and in the end no shot came.
Tshabalala tested the palms of Iker Casillas with a shot from 30-yards that the keeper beat away, only for Booth to fire in the rebound that was narrowly side.
Parker was proving a menace for the Spanish, but all too often found himself isolated and facing two or three defenders with no strike-partner to assist him.
Tshabalala again tested Casillas, this time with a much weaker right-footed shot, while at the other end Albert Riera fired wide from an angle when a square ball to Torres might have seen the striker open the score.
Mokoena glanced a header goalwards that was comfortable for Casillas as Bafana continued to have the better of the play as the first half drew to a close.
Bafana got lucky soon after halftime as Spain had the ball in the back of the net when Villa’s shot from distance was only parried by Khune, but Sergio Busquets was adjudged offside as he stuck the rebound away. Replays showed, however, he was well on, while three of his teammates were ahead of the Bafana defence.
Vicente del Bosque made a double change on 57 minutes, replacing Torres and Villa with Daniel Guiza and David Silva respectively, while Khune had to make a smart save low down from Riera and then beat away a Cazorla shot from outside the area.
Joel Santana made changes of his own, bringing on Mphela for a tired looking Pienaar on 65 minutes, while Modise was withdrawn for Elrio van Heerden five minutes later.
And the moves worked. Van Heerden played a sublime inside ball to Tshabalala, whose crossed was controlled at the second attempt by Mphela and the Mamelodi Sundowns striker fired into the roof of the net from four yards.
Santana tried to close out the match by replacing Tshabalala with the defensive midfielder Benson Mhlongo with six minutes to go, but Guiza found a controversial equaliser. The striker appeared to control the ball with his arm, before firing past Khune.
And it went from bad to worse for the home side as Guiza’s attempted cross floated over a static Khune and in off the far post, at which stage it looked all over for Santana’s men.
But a remarkable free-kick, won by Van Heereden, by Mphela from 35-yards, the goal of the tournament, left Casillas with no chance and the game went into extra time.
After the restart, Joan Capdevila again had Khune flapping from a cross, but this time it hit the post and went away from goal, while Mphela was set free by the impressive Van Heerden, but Casillas came up with the save with an outstretched boot.
Parker also forced a fine from Casillas, while Mhlongo headed over from the resultant corner as Bafana pressed for a winner.
Bafana got another lucky escape at the other end as a header from Silva struck the arm of Mhlongo, but referee Matthew Breeze from Australia waved play-on when he might have easily pointed to the spot.
Just a minute into the second half of extra time, Alonso’s free-kick into the area found its way into the back post without a touch from anybody, much to the dismay of the Bafana players.