Midfielder Bernard Parker scored the early second-half winner, chipping goalkeeper Victor Valdes from close range and a desperate lunge from a defender could not keep the ball out.
Spain rarely threatened to equalise until Alvaro Arbeloa came close deep in stoppage time at the Soccer City stadium where they defeated the Netherlands 1-0 to win the 2010 World Cup.
The shock defeat came five months after the only other reverse for La Roja (The Red Fury) this year — a 3-0 hiding from hosts Brazil in the Confederations Cup final.
"It was a fantastic result," said South Africa coach Gordon Igesund. "The players were great and our tactics were spot on.
"We knew what Spain would do and planned accordingly. The supporters needed a result like this after our failure to qualify for the 2014 World Cup."
Spain brought seven of the team that started in the World Cup final and they all began against Bafana Bafana (The Boys).
South Africa included left-back Thabo Matlaba and midfielders Andile Jali and Oupa Manyisa from the Orlando Pirates side that finished runners-up to Egyptian side Al Ahly in the CAF Champions League this month.
Striker David Villa had the ball in the South African net twice within seven minutes of the kick-off, but the assistant referee correctly ruled offside on each occasion.
The world champions dominated possession in the opening stages with goalkeeper Iker Casillas often the lone figure in the Spanish half.
But South Africa nearly snatched the lead on 18 minutes when a long-range Manyisa shot went over off the top of the crossbar after taking a deflection.
Parker then stung the fingers of Casillas with a snap shot that the captain could not grasp, but the ball was hastily cleared
Other chances saw Fernando Llorente head wide off a cross and Bongani Khumalo did likewise after a corner in first-half stoppage time.
Tokelo Rantie was wildly off target when he should have done better early in the second half after being put through by substitute Siphiwe Tshabalala.
However, that was forgotten 11 minutes into the second half when slick Rantie and Manyisa passes left Parker facing substitute Valdes.
The diminutive midfielder, who has played for Dutch, Serbian and Greek clubs, calmly chipped the ball over the shot-stopper and the ball landed in the net.
Valdes was forced to retire injured with 10 minutes left, creating a problem as Spain had used all six substitutes allowed.
But after touchline discussions, third-choice Pepe Reina was allowed to come on, much to the annoyance of Igesund.
Rantie squandered a great chance to double the lead five minutes from time as he broke clear only to allow Reina block a timid strike.