Batsman Henry Nicholls and all-rounder Daryl Mitchell also scored centuries as the hosts declared on 659-6.
Pakistan lost opener Shan Masood for a duck as they finished on 8-1 in their second innings, still 354 runs behind.
New Zealand, who won the first Test in Mount Maunganui, will become the number one ranked Test team if they complete a 2-0 series win.
The Black Caps can also go second in the World Test Championship table with a victory. The top two teams will compete in the final at Lord’s next summer.
Williamson scored 24 boundaries in his knock but rode his luck thanks to some lacklustre Pakistan fielding.
He was dropped on 82, 107 and 177 as Pakistan’s slip and gully fielders put down chances.
Nicholls was also dropped three times as he scored his seventh Test century with a well-crafted 157, including a record 369-run partnership with Williamson.
It was New Zealand’s highest fourth-wicket stand in Test cricket, and the best at the Hagley Oval ground.
As the Pakistan bowlers tired, Mitchell blasted an unbeaten 102 from 112 balls for his maiden Test ton, including eight boundaries and two sixes.
With 11 overs to face in the fading light, Masood – who had earlier dropped three catches – edged Kyle Jamieson to Tim Southee at slip to leave Pakistan facing an uphill task to take the match to a fifth day.