Williamson ended 108 not out after resuming at 78 as New Zealand progressed from 142-5 overnight to reach its winning target of 189 after only 55 minute play on the fourth morning of a match which saw dramatic changes of fortune.
Both teams seemed to squander winning positions before New Zealand finally edged to brink of victory by the end of the third day Sunday on which 16 wickets fell.
Sri Lanka, which led by 55 runs on the first innings after losing an influential toss, had made its way to 71-0 in its second innings to take command of the match when it suffered a catastrophic collapse, losing all 10 wickets for 62 runs to be out for 133.
That left New Zealand needing only 189 to win but the home side's batsmen showed the same recklessness as Sri Lanka's and it stumbled to 142-5 by stumps, having lost almost all of its top order, before Williamson asserted himself and took control.
"It was an interesting surface," Williamson said. "The Sri Lanka bowlers put us under a lot of pressure and we did the same when we learned from them.
"I think it's very important to stick to your game plan when that's going or come up with a game plan that's clear in your mind and you're able to stick to.
"It was an interesting game, I'm sure it was pretty good to watch."
New Zealand's Kane Williamson celebrates his century against Sri Lanka. (AP)
Williamson had only a little support late on the third day from Ross Taylor, who made 35, and captain Brendon McCullum, who struggled to 18, battling all the time against an apparent impulse to throw his wicket away.
The same impulse had overtaken the Sri Lanka team earlier in the day as it tried to enlarge the advantage it enjoyed when it bowled out New Zealand for 237 in reply to its own first innings of 292. Sri Lanka achieved that first innings total in difficult circumstances, after it lost the toss and had been sent in on a pitch, newly laid in Patumahoe clay, which was richly green and seaming.
With the help of captain Angelo Mathews, who suprassed 4,000 test runs on his way to the innings top-score of 73, and Milinda Sirwardana who made a mature 62, Sri Lanka defied New Zealand's four seamers to reach a total which was well over par.
How much over par was shown when New Zealand foundered against a barrage of short pitched bowling from the young Sri Lanka seamer Dushmantha Chameera in its first innings. After reaching a solid 81 without loss, New Zealand crumbled to be all out for 237 and Chameera, only 23 and in his third, captured his first five-wicket bag and finished with 5-47.
Chameera also undid New Zealand at the start of its second innings, taking 4-68 to improve his match figures to 9-113.
Batsmen from both sides showed immense vulnerability against the fast, short-pitched attacks which became the tactical resource of both captains. The new pitch provided pace and a steepling bounce that challenged the technique of all batsman. Only Mathews and Siriwardana in Sri Lanka's first innings, Martin Guptill who made 50 in New Zealand's first dig and Williamson ever showed signs of being at home in the conditions.
But a rashness, a determination to play aggressively even when conditions demanded caution, also undid most batsmen and sped the test towards its early resolution Monday.
"I thought we had a chance to win this match," Mathews said. "But I thought in the second innings we made a mockery out of our batting.
"We showed some improvement from the first test. We had a little more bounce and pace on this wicket and the character the bowlers showed, especially Chameera who is finding his feet in test cricket, was absolutely amazing."
Williamson had the composure to lead New Zealand home Monday when a further loss of wicket might have made its task an anxious one. His century, which came in 227 minutes from 152 balls was the 13th of his career and the fifth in a calendar year in which he score 1,172 runs in test — a record for a New Zealander. B.J. was with him, 13 not out at the close.
BRIEF SCORES (FULL SCORECARD)
Sri Lanka 1st Innings: 292 (Angelo Mathews 77, Milinda Siriwardana 62, Dinesh Chandimal 47, Kusal Mendis 31; Tim Southee 3-63, Trent Boult 2-51, Doug Bracewell 2-81).
New Zealand 1st Innings: 237 (Martin Guptill 50, Mitchell Santner 38, Doug Bracewell 35; Dushmantha Chameera 5-47, Nuwan Pradeep 2-39, Rangana Herath 2-75).
Sri Lanka 2nd Innings: 133 (Kusal Mendis 46, Dimuth Karunarate 27; Tim Southee 4-26, Neil Wagner 3-40, Doug Bracewell 2-31).
New Zealand 2nd Innings: 189 for 5 (Kane Williamson 108 not out, Ross Taylor 35; Dushmantha Chameera 4-68).
Result: New Zealand beat Sri Lanka by 5 wickets to win 2-match Test series 2-0.