Centuries by AB de Villiers and Jacques Kallis put South Africa back on track after Pakistan's pace bowlers had ripped through their opening batsmen on the first day of the second Test on Sunday.
South Africa were 311 for five wickets at the close with De Villiers not out on 120.
At 33-3, South Africa had looked to be in trouble but a spirited 179-run partnership between Kallis and De Villiers bailed them out of trouble.
Kallis raced to his quickest Test century, his 37th, before falling to Tanvir Ahmed on 105 just after tea.
"It was hard work this morning but I was pleased with how I was moving," Kallis told reporters.
"We have to try and get to 400 and put the pressure back on Pakistan," he added.
Pacer Tanvir, making his Pakistan debut, took four wickets for 67 runs and confessed that nerves had kept him awake on the night before the game.
"I didn't sleep all night as I was thinking how I would be bowling to Jacques Kallis and Hashim Amla," Tanvir, 31, told the post-match news conference.
"I am thankful to God that I finally made my debut. I never expected such a performance," added Tanvir, the oldest debutant for Pakistan, whose victims included Amla for four runs.
Ahmed was part of the squad that toured England earlier this year but he did not get a chance to play.
"The time I spent with senior team members over the last few months helped me a lot in my performance," he said.
Captain Misbah ul-Haq put the Proteas in to bat and relied on his pace bowlers to use the moisture and grass on the wicket.
Ahmed struck in his first over, sending back Alviro Petersen (2), caught at slip by Ul-Haq.
Five balls later, he took the wicket of Amla in a controversial decision as replays showed the ball came off Amla's thigh before being caught at slip.
In the next few overs, he took the wicket of South African captain Graeme Smith (10).
The first Test in Dubai last week ended in a draw.
Scorecard:
South Africa first innings
G. Smith c Akmal b Ahmed 10
A. Petersen c Misbah b Ahmed 2
H. Amla c Akmal b Ahmed 4
J. Kallis b Tanvir Ahmed 105
AB de Villiers batting 120
Ashwell Prince c Shafiq b Hafeez 32
M. Boucher not out 26
Extras (b-6, lb-1, nb-5) 12
Total (five wickets, 90 overs) 311
Fall of wickets: 1-2 2-6 3-33 4-212 5-268
Bowling: Umar Gul 21-5-60-0, Tanvir Ahmed 16-2-67-4, Mohammed Sami 14-1-69-0, Younis Khan 3-1-11-0, Abdur Rehman 29-7-77-0, Mohammad Hafeez 7-0-20-1