Hasan Ali ripped out South Africa's lower order before the visitors' spinners kept them in an intriguing contest with Pakistan.
Hasan (5-54) claimed the second five-wicket haul of his Test career as the Proteas were bundled out for 201, giving Pakistan a first-innings lead of 71 on day three of the second Test.
Keshav Maharaj (2-74) and George Linde then shared five wickets to prevent the hosts from pulling away, although Faheem Ashraf (29) and Mohammad Rizwan (28 not out) combined for a vital sixth-wicket stand of 52.
Slow left-armer Linde (3-12) persuaded Faheem to slice to Anrich Nortje at backward point in the penultimate over, but a score of 129-6 at stumps means Pakistan's lead is 200 on a surface already displaying variable bounce.
South Africa resumed on 106-4 in their first innings, with plenty of their hopes pinned on Quinton de Kock. Unfortunately, the captain could only add five to his overnight 24 before being bowled when driving at Shaheen Afridi.
Afridi was also responsible for running out Wiaan Mulder, who pushed for two down to fine leg and departed for 33, ending a 49-run stand with Temba Bavuma, who was left high and dry on 44 not out.
That was largely down to Hasan, who bowled Linde for 21 before rearranging Maharaj and Nortje's stumps.
Kagiso Rabada became the fourth South African run out in the series but made amends by trapping Imran Butt lbw for a duck. That was part of a masterful new-ball spell with Nortje as the South Africa pacemen began the innings with 25 dot balls.
Maharaj still came into the attack early and wheeled through 21 of the 51 overs bowled, having Abid Ali caught behind sweeping for 13 and pinning Babar Azam lbw for eight - the third time in four innings the Pakistan captain has fallen to South Africa's premier spinner.
Linde was able to offer Maharaj far more effective support this time around, having lacerated the little finger on his bowling hand during the first innings, and had Azhar Ali leg before for 33 and Fawad Alam pouched at short leg.
PROFLIGATE PROTEAS
After Nortje - operating at a blistering pace just shy of 150 km/h - and Rabada put the shackles on Pakistan and Maharaj and Linde got among the wickets, a route to a series-levelling victory was starting to open up in Rawalpindi. However, Faheem was dropped on nought by Dean Elgar at slip off Linde and next ball Rizwan edged Maharaj to Rassie van der Dussen at silly point, only to be similarly spared. In the final analysis of a low-scoring contest, those mistakes might look absolutely dire.
TOUGH START AT THE TOP FOR BUTT
Butt's exit without troubling the scorers continued a meagre start to the 25-year-old's Test career. A first-innings 15 here is his best effort in four attempts. There are few sterner examinations to be had for Test openers as when Rabada and Nortje are in the mood; however, Butt must earn the right to sail in less choppy waters.