Guptill picked up where he left off in the one-day international series by smashing 58 off 34 deliveries, while Williamson compiled a 42-ball 53 in New Zealand’s imposing 182 for four.
The Black Caps looked set to enjoy a comfortable win after leaving Sri Lanka 42 for four but the game was in the balance when the tourists required 35 from 24 balls with four wickets in hand.
Trent Boult returned to claim the vital wicket of Milinda Siriwardana as he finished with 3-21 and Grant Elliott held his nerve to deny Sri Lanka the 13 runs they required from the final over.
New Zealand were on course for a total in excess of 200 after reaching the halfway point of their innings on 97 without loss.
But the run out of Guptill checked the home side’s momentum before Sri Lanka removed Williamson and Corey Anderson in quick succession to leave New Zealand 135 for three at the start of the 17th over.
Ross Taylor got the Black Caps going again with a flurry of boundaries and Craig Munro followed suit to set the tourists a challenging total.
Sri Lanka’s run chase got off to a nightmare start with Tillakaratne Dilshan, Dinesh Chandimal, Shehan Jayasuriya and Angelo Mathews all falling in the first five overs, but Danushka Gunathilaka stood firm at the other end to give his side a fighting chance.
Sri Lanka were 84 for five in the 10th over when the opener was dismissed by Ish Sodhi for 46 and Siriwardana and Thisara Perera continued the revival by guiding the tourists to 133 in the 15th.
Matt Henry broke the partnership by removing Perera on his way to figures of 3-44 and there was no way back for Sri Lanka when Boult had the dangerous Siriwardana caught in the deep.
Chandimal’s side finished on 179 for nine and must now win Sunday’s second T20I to earn a series draw.