Williamson, who resumed on the fourth day on 14, batted the entire day and grafted an unbeaten 139 off 282 balls with 13 fours.
Left-hander Nicholls overcame a jittery start to be 90 not out off 243 deliveries as New Zealand overcame being 60-4 in the morning to reach 272-4 at stumps and regain the advantage. Pakistan let it slip away through lapses in the field and taking a defensive approach.
New Zealand were still trailing Pakistan in their second innings by 14 runs when Nicholls joined Williamson before lunch. But they batted with determination and luck in an unbroken 212-run, fifth-wicket stand to leave Pakistan worried about what total it may be chasing to win the series on the last day Friday.
New Zealand will be in charge and favoured for the series win when it will probably decide to end their second innings and leave enough time to bowl out Pakistan.
Pakistan does not have happy memories at Abu Dhabi's Sheikh Zayed Stadium, where New Zealand won the first Test by just four runs after setting Pakistan a target of 176.
Pakistan made a perfect start to the fourth day when legspinner Yasir Shah became the quickest bowler to reach 200 Test wickets in his 33rd test by dismissing nightwatchman Will Somerville for 4.
But Shah dropped Williamson on 80 and 106.
And Imam-ul-Haq couldn't hold onto a sharp reflex catch of Nicholls close to the wicket on the leg side soon after the left-hander completed his half-century before tea.
Shah could have dismissed Nicholls on 3, but Pakistan didn't go for the leg before wicket referral. Video replays showed the ball would have crashed onto the leg stump.
Nicholls survived another chance on 9 when he successfully overturned an lbw decision against him off Shah's sharp turning delivery from round the wicket.
New Zealand resumed on 26-2 and Shah got Somerville to reach 200 wickets and break the 82-year-old record of Australia legspinner Clarrie Grimmett, who got his 200th wicket in his 36th test in 1936 against South Africa at Johannesburg.
Ross Taylor's overly aggressive approach ultimately led to his downfall on 22 off 14 balls when he was caught in the deep as New Zealand slumped to 60-4.
Taylor smashed three boundaries in Shaheen Afridi's one over and was undone by Afridi's pace in the next over and holed out at deep mid-wicket.
Williamson and Nicholls then dug in well on a slow pitch and gradually increased the lead as Pakistan fielders let chances slip away.
Williamson completed his 19th test century off 154 balls with a crisp cover-driven boundary off Hasan Ali just before tea. By stumps, he'd endured 100 overs.
He and Nicholls, closing on his third test century, scored 87 in the second session and 73 in the last session. Pakistan had a chance to take the new ball over the last 24 overs but declined, setting defensive fields to keep down New Zealand's run rate.
The run rate came down, but Williamson and Nicholls still took the game away from a tired Pakistan attack.