South Africa reached 66 for two at tea, still needing 116 runs for victory on day four of the second Test at Newlands, in Cape Town, on Sunday.
Having bowled out Pakistan for 169, in the sixth over after lunch, the hosts faced an initial target of 182.
They lost the early wicket of Alviro Petersen (one), trapped leg before wicket when an Umar Gul delivery thudded into his back pad, in line with middle stump.
Petersen, who had survived a catch after Mohammad Irfan's foot had gone over the line, was on his way with only 10 runs on the board.
Graeme Smith and Hashim Amla shared a 53-run second wicket stand, off 72 balls, and were just starting to motor when Smith (29), attempting the sweep, was out lbw to Saeed Ajmal.
At tea, Hashim Amla was on 32 and Jacques Kallis on three not out.
Resuming the day at 100 for four, the visitors lost the wicket of captain Misbah-ul-Huq for 44. He attempted to reverse sweep Peterson and, after adding only six runs to his overnight total, he looped the ball to Smith at short-fine leg.
Asad Shafiq (19) was unlucky when he played a defensive shot to Vernon Philander and it rebounded backwards onto his stumps.
The Proteas then took three wickets with three consecutive balls as Pakistan began to crumble.
Peterson took the first of them with the last ball of his over and Sarfraz Ahmed could only look on in disbelief as he left the delivery alone. He turned around and saw the ball, pitched outside leg, swing back onto the stumps, and he was on his way for five.
Overnight stalwart Azhar Ali went out to the first ball of Philander's next over, edging to a diving AB de Villiers for a well-played 65.
Off Philander's next delivery, Petersen, at third slip, reacted quickly to take a brilliant two-handed catch to his right and see off Umar Gul who faced just the one ball.
With Philander on a hat-trick, Saeed Ajmal played sensibly to block the next delivery.
Needing two more wickets after lunch, Robin Peterson bowled Ajmal (four)
around his legs and Dale Steyn finished off the job by removing Irfan (two), also caught by Petersen running back from third slip.
Philander bagged four for 40 while Peterson and Steyn each finished with three scalps.