Pakistan were 308 for eight — a lead of 88 — at the close of play on Wednesday on the second day of the opening Test against South Africa in Karachi.
Resuming at 33-4, Pakistan's recovery was led by Fawad Alam (109) who scored his first Test century at home, Faheem Ashraf (64), and Azhar Ali (51).
Fawad reached the milestone, his third tonne in Test cricket, with a six over long-on to left-arm spinner Keshav Maharaj.
Earlier, Azhar Ali and Alam led Pakistan's resistance against South Africa after the hosts began the day four down with a paltry 33 runs on the board.
Playing his first Test at home, Fawad was rock-solid as he added 94 with former skipper Azhar for the fifth wicket, leaving South Africa without a scalp in the first session.
Pakistan added 71 in the first session before spinner Keshav Maharaj broke through by dismissing Azhar for 51, caught behind by Quinton de Kock.
Azhar struck four boundaries in his 151-ball knock.
Wicketkeeper Mohammad Rizwan and Alam added another 55 for the sixth wicket before the former was caught at slip off paceman Lungi Ngidi.
It was a hard going for South Africa in the first session.
Kagiso Rabada, who took two wickets for just eight runs late on Tuesday, looked threatening in his six-over spell, but Azhar and Fawad batted cautiously to take Pakistan past 100.
Both batsmen survived scares, with left-arm spinner George Linde coming close to trapping Azhar leg-before on 29 — the umpire's not-out decision upheld on review.
Fawad was dropped on 35 by Dean Elgar, who failed to hold a sharp edge off-spinner Keshav Maharaj in the slips.