Pakistan were 17 for no loss at the interval with Mohammad Hafeez batting on 7 and Kamran Akmal on 10.
Wicketkeeper Akmal opened after regular opener and vice-captain Salman Butt was sent to hospital after complaining of severe stomach pain.
South Africa appeared set for an impregnable total after going to lunch on 385 for five but lost their last five wickets for 58 runs.
Left arm spinner Rehman took four wickets for 24 runs in 35 balls, despite a brilliant 77 from AB de Villiers.
Rehman is the first left-arm spinner to play a Test for Pakistan since Nadeem Khan appeared in the Kolkata Test against India in March, 1999.
Rehman ripped through the South African tail with Villiers the last man out, undone by a superb Yorker from Umar Gul.
Villiers was in fine form, spanking seven fours and a six in his 101-ball innings.
Rehman had Mark Boucher caught bat and pad by Akmal and 16 runs later got Andre Nel to give a catch to Misbah-ul-Haq at silly mid-off.
Paul Harris, the lone spinner in the visiting side, was also caught behind. Rehman's fourth wicket came when he bowled Dale Steyn.
Hafeez and Akmal survived a torrid spell from Makhaya Ntini and Steyn, with the ball alarmingly low at times.
Leg-spinner Danish Kaneria took his 200th Test wicket before lunch when he held a return a catch from Ashwell Prince (36) having earlier dismissed Jacques Kallis, who fell for 155.
Kaneria was the sixth Pakistani to reach the 200 Test wickets mark.