The victory tied the series at 1-all.
Batting first the Black Caps were all out for 108 with opener Will Young top scoring with 30, while Mitchell Santner (19) and Josh Clarkson (24) helped boost the total.
It wasn't a big number to defend, but the New Zealanders bowled and fielded brilliantly, dismissing Sri Lanka for 103 in the final over.
Key to it was Ferguson's hat-trick when Sri Lanka were 24 for one. He got Kusal Perera out with a yorker that caught the outside edge of the bat for Mitch Hay to take the catch on the last ball of the sixth over. It was upheld on review.
With the first ball of the eighth over, Ferguson claimed an LBW decision off Kamindu Mendis which the Sri Lankans unsuccessfully reviewed, while the next ball to skipper Charith Asalanka also attracted an edge, with Hay completing the catch. Once again, the Sri Lankans reviewed, but there was a clear nick on replay.
A partnership between Pathum Nissanka and Bhanuka Rajapaksa added 29 runs for the fifth wicket, swinging momentum back to Sri Lanka, before Michael Bracewell got rid of Rajapaksa with Hay taking the catch on the second attempt.
The hosts were always a chance with Nissanka at the crease, and they started the final over at seven down needing eight runs to win.
Nissanka went for a big hit off the second ball from Glenn Phillips, but Henry Nicholls took the catch comfortably in the deep, and Phillips got Matheesha Pathirana out with the next delivery. Then with Sri Lanka needing six to win off the final two balls, Theeksana got a top edge and Hay took a running catch to seal the win.
Player of the match Ferguson finished with three for seven off just two overs, while Phillips took three for six off 1.5 overs. Ferguson's hat-trick was the fifth by a New Zealand bowler in men's T20 internationals.
It was a big match for 24-year-old Hay, whose six dismissals from five catches and a stumping, was the most by a Black Caps keeper in the format.
Ferguson, who left the field with a calf injury after his overs, said it took a full team effort to get the win. It was the lowest total that the Black Caps had successfully defended during his time with the side.
"GP (Phillips) bowled a lovely death (over) to get us over the line," he told Sky Sport.
Ferguson said his job was ''to try to run in quick and get some some poles for the boys, so I was happy to get a couple tonight.''
Tour captain Santner praised Ferguson as a ''world-class bowler''.
"Any time someone can bowl thunderbolts like that it's not easy to face. That was his role tonight, to try get a few wickets in the middle and break up the spin a little bit so that was nice."
Santner told Sky that while the New Zealand total was lower than they would have wanted, it was a tricky wicket and he had asked the players to fight hard for the win.
The two sides now play a three-match ODI series, starting in Dambulla on Thursday.