Spinner Simon Harmer took three wickets to leave Pakistan 4-94 at stumps in their second innings on Wednesday, with a slender 23-run lead, but it was the comeback from the South African tail that enabled the tourists to move on top with two days left.
Seerun Muthusamy, coming in at No.7, scored an unbeaten 89 and last man in Rabada a maiden Test 50 as South Africa's lower order hauled them out of trouble and secured a 71-run lead after they had scored 404.
South Africa started the day on 4-185, trailing by 148 runs, and were in trouble when they lost three wickets in the first hour to be 7-210, well behind Pakistan's first innings 333.
But Muthusamy led a comeback as they overhauled the home team's total.
He was stranded 11 runs short of a maiden Test century while Rabada's 71-run knock, which ended as he was caught slogging, was his highest Test tally. It was only the third time a South African No. 11 had scored a Test half-century.
The pair put on 98 for the last wicket after Muthusamy and Keshav Maharaj also enjoyed a 71-run partnership for the ninth wicket. Maharaj was stumped for 30.
The home side snagged their first wicket with the fourth ball of the day, with their 38-year-old debutant Asif Afridi going on to post figures of 6-79.
But his performance was overshadowed by the tourists' tail, which scored 119 runs in the middle session and took the run rate up to 4.67 before being finally dismissed at tea.
Pakistan's reply was full of nerves, with their opening trio of batters removed cheaply, to be reeling at 3-16 before highly rated Babar Azam came to the crease to wild applause at the Rawalpindi Cricket Stadium.
He will be key to Pakistan's chances on Thursday, as he is unbeaten on 49 with Mohammad Rizwan on 16. Pakistan are ahead in the two-Test series after winning by 93 runs in Lahore last week.