South Africa were 32 runs shy of winning the first Test while Pakistan needed only two wickets for success as the contest headed to a thrilling finish on the fourth day at Centurion on Sunday.
Mohammad Abbas bowled Pakistan to the brink of an extraordinary win as he took four wickets in a marathon spell of 13 successive overs as South Africa made heavy work of chasing a modest target of 148.
They were 116-8 at lunch with their hopes pinned on the fast bowling pair of Marco Jansen (five) and Kagiso Rabada (10), who were not out at lunch.
Abbas, whose overall figures were 6-43 off 17 overs, engineered a dramatic collapse after South Africa looked headed for victory with 50 runs needed and six wickets in hand.
His haul included the fortunate dismissal of captain Temba Bavuma for 40, who walked after thinking he had edged the ball behind to the wicket keeper but will be ruing not reviewing the decision as the television replays showed it had not touched his bat but rather his pocket on the way through.
Abbas also bowled Aiden Markram and had David Bedingham and Corbin Bosch caught behind — Bosch going first ball to see South Africa slump from 96-4 to 99-8.
Pakistan are on the brink of their first Test victory in South Africa in 18 years while the home side are chasing a victory that will secure a place for themselves in the World Test Championship final in June.
Set a modest target to win the first of a two-test series, South Africa were 27-3 overnight but were looking odds-on for victory as Bavuma and Markram advanced the score in a positive start on Sunday.
But Abbas, back in the team after a three-year absence, turned the tide in Pakistan’s favour as he bowled Markram for 37 before Bavumas unusual dismissal.
The home collapse then saw Kyle Verreynne (2) bowled by Naseem Shah and Bedingham (14) and Bosch try to drive rising deliveries from Abbas but both edge to wicketkeeper Mohammad Rizwan.