Connect to share and comment
South Africa were 65 for two at tea after dismissing Pakistan for 338.
It was a productive day for Ajmal, who helped fellow tailender Tanvir Ahmed put on 64 for the ninth wicket to take Pakistan comfortably past the 300 mark.
Their eventual total had seemed unlikely when Vernon Philander took three wickets in his first three overs to reduce the tourists to 268 for eight.
Ajmal came on to bowl after 11 overs had been bowled in South Africa's innings.
He struck with his ninth delivery when South African captain Graeme Smith missed a sweep.
Umpire Steve Davis turned down Ajmal's appeal for leg before wicket but the Pakistanis sought a review which showed the ball had pitched in line and would have hit the stumps.
Two overs later Ajmal had Alviro Petersen smartly caught at short leg by Azhar Ali, who dived to his right to hold a sharp chance. At tea, Ajmal had taken two for 20.
Faf du Plessis surprisingly batted at number four for South Africa in place of Jacques Kallis.
Pakistan batted until lunch after resuming on 253 for five, although Philander's early strikes raised the home side's hopes of wrapping up the innings quickly.
Philander, who took five for 59, struck with his first ball of the day when Asad Shafiq was caught at first slip without adding to his overnight score of 111. Shafiq batted for 308 minutes and faced 230 balls.
Sarfraz Ahmed, the other overnight batsman, fell in Philander's next over when Petersen dived full length to his right to hold a good catch at third slip. Sarfraz made 13.
Umar Gul was leg before wicket without scoring as Philander completed his ninth five-wicket haul in Test matches.
But Tanvir and Ajmal went for their strokes, with Tanvir in particular hitting out aggressively as he made 44 off 59 balls with four fours. Ajmal made 21 not out.
South Africa suffered a blow when fast bowler Morne Morkel left the field after bowling three balls of his 21st over with what was described as a tight left hamstring.
Tanvir fell shortly before lunch when he hit left-arm spinner Robin Peterson to Philander at deep mid-off, with last man Mohammad Irfan following in the next over when he was bowled by Peterson, going for a big hit.