Williamson century (129), along with important contributions from Ross Taylor (70), Henry Nicholls (56) and BJ Watling (73) powered the hosts to 431 before Pakistan went to stumps on 30/1 with Abid Ali (19*) and nightwatchman Mohammad Abbas in the middle.
Kyle Jamieson drew the first blood for New Zealand as the visitors lost Shan Masood (10), who looked set at the crease, after he faced 42 balls.
Earlier, New Zealand resumed the day on 222/3, with skipper Williamson and Nicholls in the middle. It was a good start to the morning session as Nicholls brought up his fifty in the second over of the day before the partnership between him and Williamson passed 100 in the sixth over.
Just two overs after that, the New Zealand skipper reached his ton, his 23rd in Test cricket, as the home side continued to build a strong total.
However, after drinks, 17-year-old Naseem Shah managed to get the breakthrough as he had Nicholls (56) caught off a short ball by Shan Masood. Pakistan then got the prized wicket of Williamson (129) as he nicked one to Haris Sohail off the bowling of Yasir Shah to leave the Black Caps on 281/5.
New Zealand reached 301/5 at lunch but not long after the interval, Faheem Ashraf had Mitchell Santner (19) playing and edging to Mohammad Rizwan. In came Kyle Jamieson and the big man hit back-to-back boundaries off Shah, including a big six, as the hosts looked to give the score a quick boost.
Jamieson, who was averaging 54 with the bat before this game, shared an important 66-run stand with Watling. Their solid stand came to an end shortly before the tea break, when Mohammad Abbas had him caught behind for 32.
Pakistan claimed the wicket of Tim Southee on the first ball after the tea break courtesy of a brilliant googly from Yasir Shah.
A few overs later, Neil Wagner got a lifeline. Afridi had shattered the stumps with a perfect yorker but it was declared a no-ball for overstepping. In the same over, he survived an LBW decision after the ball-tracking showed it was missing the wickets.
After missing out on Wagner’s wicket, Afridi struck once again to claim the important wicket of Waling, who was caught in the gully by Yasir. The New Zealand wicket-keeper batsman played a solid knock, and his 145-ball innings featured eight fours.
Brief Scores: Pakistan 30/1 (Abid Ali 19*, Shan Masood 10, Kyle Jamieson 1-5) vs New Zealand 431 (Kane Williamson 129, BJ Watling 73, Shaheen Afridi 4-109).