Backed by lusty hitting from Corey Anderson and Luke Ronchi, New Zealand overtook Sri Lanka's 276 with 11 balls to spare and move 2-1 ahead in the seven-match series.
In a tense finish New Zealand needed 47 off 36 balls when the 24-year-old Williamson was removed for 103, his fifth ODI century.
Anderson was run out for 47 with New Zealand needing 32 runs with 26 balls left.
Up stepped Luke Ronchi to blast a rapid 32 including three sixes and Daniel Vettori hit a four as New Zealand finished with a flourish.
More From This Section
He brought up his century with a steady accumulation of singles, only reaching the boundary seven times with six fours and a six.
But having attained the milestone he was bowled by Thisara Perera and Anderson vacated the field soon after.
McCullum, on whom New Zealand rely heavily to get off to a good start, failed to oblige this time with a gentle poke at a Nuwan Kulasekera delivery and was caught at mid on for just 11.