Pakistan took three priceless wickets in a devastating 25-minute burst before the close to leave South Africa in deep trouble in the first Test.
A gripping third day of the clash in Karachi looked set to be one that ended with South Africa in healthy shape in their second innings, but the impressive Yasir Shah had other thoughts and helped reduce the tourists to 187-4, for a slender lead of 29 runs.
The losses of Rassie van der Dussen (64), Faf du Plessis (10) and Aiden Markram (74) in that rush of wickets meant the advantage was firmly with Pakistan at the close.
In the first of two Tests, Pakistan had earlier frustrated their visitors with the bat when a wagging tail saw them move from 308-8 at the start of Thursday's play to 378 all out.
That gave Pakistan a first-innings lead of 158, not bad going for a team who had been 27-4 at one stage.
Kagiso Rabada removed Hasan Ali in the morning, smashing his middle stump out of the ground, and the South Africa paceman's figures of 3-70 took him to 200 Test wickets.
But Pakistan's lower order largely showed impressive defiance, and number 11 batsman Yasir, who has a Test century to his name, was stranded on 38 not out when Nauman Ali (24) was last man out.
Yasir would soon get his leg breaks turning and removed Dean Elgar for 29 to break up South Africa's opening partnership.
Markram, who has enjoyed hot spells with the bat on home soil, then looked to have chosen an opportune moment to make a first Test half-century outside South Africa, but the events of the final half-hour saw Pakistan wrest back control.
Abid Ali took a sharp catch at silly mid-off to give Yasir the important wicket of Van der Dussen, ending a 127-run second-wicket partnership.
Du Plessis had an early life when a review spared him an lbw dismissal, the ball from Yasir shown to have pitched millimetres outside leg stump.
But Yasir would not be denied for long and soon had his man pinned in front again, with no doubts second time around.
Markram was prised out by Nauman moments later, prodding to silly mid-off, as the complexion of the contest changed completely.
Yasir a game-changer
First came his quickfire runs from number 11, the sort of innings that causes opponents deep frustration, but then we saw Yasir at his best with the ball. Markram and Van der Dussen looked to be leading South Africa to a healthy position - and to be only one down and back in the black at the start of Friday's play would have been a huge boon. But Yasir (3-53) kept his focus sharp and his deliveries repeatedly hitting the mark in those closing overs, and the rewards deservedly came his way.
Breakthrough for Markram
The South Africa opener has four Test centuries but until this innings he had curiously never posted a fifty outside his home country in the five-day game. It was a purposeful, positive knock and looked set to see him grab the day's headlines, until Yasir put the Proteas in a spin. With wickets falling at the other end, Nauman seized the moment to prise out a presumably distracted Markram and land perhaps the most telling blow of all.
Hope for tourists?
If Pakistan were able to battle back from 27-4, then why shouldn't South Africa, who are 29 runs ahead in the match but also four down, also find runs in their middle and lower orders? Temba Bavuma and Keshav Maharaj have recent Test half-centuries, and the influence of captain Quinton de Kock, who resumes on nought on Friday, could be all-important.