Pakistan set South Africa a target of 277 runs to win the first Test after being bowled out for 167 in their second innings at the Gaddafi Stadium on Tuesday.
Babar Azam top-scored for Pakistan with 42 runs, while Abdullah Shafique made 41, Saud Shakeel 38, Mohammad Rizwan 14, and Nauman Ali 11.
Shan Masood was dismissed for 7, Salman Ali Agha for 4, Sajid Khan for 1, while Imam-ul-Haq and Shaheen Shah Afridi fell without scoring.
For South Africa, Senuran Muthusamy was the pick of the bowlers, claiming five wickets in the second innings.
Simon Harmer grabbed four wickets, while Kagiso Rabada managed to take one wicket
Earlier, South Africa were bowled out for 269 in their first innings.
Resuming on 216 for six on the third day, the visitors managed to add just 53 runs before losing their remaining wickets.
Tony de Zorzi top-scored for South Africa with a gritty 104, while Ryan Rickelton made 71.
From the home side, Nauman Ali starred with six wickets, while Sajid Khan took three and Salman Ali Agha chipped in with one.
South Africa now needs 277 runs to win the first Test match of the series with two days remaining.
The Test match series is part of the ICC World Test Championship 2025-27 and will be followed by six white-ball matches (three T20Is and three ODIs) between the two sides from October 28 to November 8.