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.”
Pacer Ahmed, 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,” Ahmed, 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 Ahmed, 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.