While the honours were even in the first session, Pakistan took control in the second following a resolute performance by their top order. But the final session saw the hosts make a terrific comeback thanks to Duanne Olivier, who picked up another five-wicket haul to finish the game with 11 scalps.
Quinton de Kock's efforts to fetch his side a handful 42-run lead proved to be of little use initially when Fakhar Zaman and Imam-ul-Haq started off in fine fashion. While Zaman was watchful, Imam pushed the scoring rate courtesy four boundaries behind the wicket. The pair erased the deficit before Zaman was dismissed against the run of play when he smashed a leg stump half volley straight to fine leg. The wicket didn't deter the visitors with Shan Masood batting cleverly alongside Imam.
Pakistan had all the momentum going their way at the tea break, leading by 58 runs. The final session though began on a disastrous note with Imam getting dismissed in the very first over. Unable to tackle the rising ball, Imam saw an inside edge rattling the stumps, much to the home side's delight. Olivier struck again, for the third time in the innings when a short ball was reluctantly pulled by Azhar Ali, only for the top edge to be pouched by a running Kagiso Rabada.
The onus was now on the experienced Asad Shafiq to stem the rot but after surviving a lbw call with a review, the right-hander went after a wide away moving delivery from Steyn to edge it to the keeper. Babar Azam, who had a fruitful outing in the first innings, was the next to depart as a peach from Rabada shattered his offstump. The slide continued with Sarfraz registering his second duck, leaving Masood with plenty to do.
The left-hander looked in terrific control and had managed to tackle the short ball to perfection against South Africa's menacing pace attack, even pulling OIivier for a six at one point. While wickets kept tumbling at regular intervals at the other end, Masood's attempt to go for another six over cow corner led to his dismissal as South Africa cleaned up the tail after a small, jittery period of play in which Mohammad Amir throw his bat around for a handful of runs. 15 wickets on the opening day, 15 wickets again on the second day and only if 10 more falls on the third, Pakistan can manage to avoid a defeat that looks certain.
Brief scores: Pakistan 181 & 190 (Shan Masood 65, Imam-ul-Haq 57; Duanne Olivier 5-59, Kagiso Rabada 3-47) lead South Africa 223 (Temba Bavuma 53, Quinton de Kock 45; Mohammad Amir 4-62, Shaheen Afridi 4-64) by 148 runs.