A dropped catch and a controversial umpiring decision thwarted Pakistan as South Africa made progress towards victory on the third day of the first Test at SuperSport Park on Friday.
Chasing a target of 149, South Africa were 81 for one at lunch after a tense morning.
Pakistan made an ideal start when Hasan Ali trapped Aiden Markram leg before wicket before a run had been scored.
Hasan and Mohammad Amir did not concede a run off the bat until Hashim Amla clipped Hasan for four in the sixth over.
There were two crucial incidents with the total on 16, either of which could have swung the game in Pakistan's favour.
Amla, on eight, was dropped by Fakhar Zaman at third slip off Hasan when he slashed hard at the ball, which flew at chest height to the fielder.
In the next over, Dean Elgar, on four, edged new bowler Shaheen Shah Afridi low to first slip where Azhar Ali appeared to take a diving catch.
On-field umpires Bruce Oxenford and Sundaram Ravi conferred before referring to television umpire Joel Wilson with a 'soft' signal of out, indicating they thought it was a clean catch.
After looking at several replays including super-slow close-ups, Wilson ruled the ball had bounced and Elgar was reprieved.
The decision clearly astonished the Pakistan players and coach Mickey Arthur was seen leaving his place on the players' balcony and going to the adjacent office of match referee David Boon.
Television commentator Michael Holding criticised his fellow West Indian Wilson.
"I'm convinced he was out," said Holding.
"I think the third umpire made a mistake. The protocol says you have got to be 100 percent sure the decision was wrong to change it."
In a similar situation, Indian captain Virat Kohli was given out in the recent second Test against Australia in Perth when television umpire Nigel Llong found no conclusive evidence to overturn a 'soft' signal from the on-field umpires.
Elgar and Amla survived some testing bowling from the Pakistan seamers and gradually began to lift the scoring rate. Elgar was on 32 and Amla 45 at lunch.