Pakistan's spinners Yasir Shah and Nauman Ali grabbed three wickets in the space of ten runs to halt South Africa's fightback on the third day of the first Test in Karachi on Thursday.
When it looked like South Africa would end the day at parity after wiping out Pakistan's big 158-run first innings lead, Yasir struck twice and Nauman once to leave the tourists on 187-4 at close.
Nightwatchman Keshav Maharaj was unbeaten on two and skipper Quinton de Kock was yet to score, as South Africa now lead by 29 runs with six second innings wickets in hand.
Earlier, Pakistan scored 378 to take a crucial lead over South Africa's first innings total of 220.
Opener Aiden Markram, who scored a polished 74 for his 8th fifty, added 127 for the second wickets with Rassie van der Dussen who scored a solid 64.
But Yasir, with figures of 3-53, changed the scenario by dismissing Dussen caught and then trapped Faf Du Plessis leg-before for ten, while Nauman had Markram caught by a close-in fielder to trigger a three wicket collapse in 33 balls.
Markram, dropped on 27 by Pakistan skipper Babar Azam off spinner Yasir, also had a leg-before decision overturned on four.
Dussen also had a leg-before decision overturned on 56. Markram hit ten boundaries and Dussen five.
Both Yasir and Nauman will be major threats for South Africa on a fourth day National stadium pitch which produced 14 wickets on day one, four on the second and six on the third.
South Africa were off to a confident start with Markram and Dean Elgar defying Pakistan's attack, and were 37 without loss at lunch.
Elgar, in visible discomfort after being hit on the left hand by a rising delivery from pacer Shaheen Shah Afridi, was caught by diving wicketkeeper Mohammad Rizwan off Yasir for 29.
In the morning Pakistan's tail added 70 runs to frustrate South Africa, who were looking for quick wickets.
South Africa's pace spearhead Kagiso Rabada bowled Hasan Ali for 21 to reach 200 wickets in his 44th Test.
Rabada, 25, is the eighth South African to take 200 or more Test wickets.
Pakistan were 323-9 at that point, but Yasir and Nauman added 55 valuable runs for the final wicket.
Yasir, who scored a Test century in Australia in 2019, hit a six and four fours in his 38 not out, while debutant Nauman scored three boundaries before falling for 24.
Rabada with 3-70 and spinner Maharaj, who took 3-90, were the pick of the South African bowlers.