New Zealand, with four new caps in their 17, needed three tries in the final quarter, including two to debutant winger Bryson Goodwin, to kick clear after the scores were locked at 24-24.
Skipper Benji Marshall gave credit to the Tongans for their performance.
"They came out pretty tough and at the start of the second half really gave us a shock," he told Skysport.
"It was a great hit-out for us in terms of experience for the boys and just to get a bit of timing back."
While both sides were using the fixture as a build-up for different campaigns -- the Tongans are heading to the Pacific Cup -- the match was sternly contested.
Tempers flared briefly early in the second half, when Marshall and opposition skipper Feleti Mateo tangled.
In a separate incident shortly after, New Zealand forward Adam Blair was put on report for a dangerous tackle on former Kiwi Epalahame Lauaki.
Mateo praised his team's display against the heavily favoured Kiwis.
"The boys should hold their heads up high, it was a great effort," he said.
"It was a stepping stone. We didn't want to take this game lightly and none of the boys did." The Kiwis began with some aggressive defence, but it was Tonga who surprisingly hit the front after six minutes.
They got field position from a 40-20 kick by skipper Feleti Mateo, who finished off the attack by forcing his way over.
On a tough night for goalkickers, with rain making conditions slippery, halfback Eddie Paea missed the conversion from virtually in front.
The Kiwis responded with three quick tries.
Hooker Issac Luke, in one of several runs that produced good metres from dummy half, set up the first for Marshall.
Centre Junior Sau, one of the Kiwi debutants, got the second with a strong run, after having forced a turnover with a strong tackle.
When winger Sam Perrett got the third with only 16 minutes gone, the Kiwis appeared to be running away with the game at 16-4.
But Tonga struck back when a great bust by Wests Tigers second rower Viliami Mataka sent winger Sam Huihahau over.
The visitors appeared to have got another when their other winger, Parramatta's Etuate Uaisele, dotted down.
But in what seemed like a tight call, centre Suiatonga Likiliki was ruled offside earlier in the move.
The Kiwis took advantage of the reprieve with halfback Nathan Fien and the centre Steve Matai both crossing before halftime for a 24-8 lead.
However, Tonga, with back-up dummy half Joel Taufa'ao influential off the bench and helped by a string of penalties, showed their resolve.
They shocked the Kiwis with three quick tries early in the second half to draw level.
Uaisele got the first two, and was unlucky not to get another when a tight knock-on decision went against Tonga.
Mataka got the third and suddenly a huge upset appeared to be possible.
However, a Fien kick produced a second try for Marshall to push the Kiwis ahead again.
Perrett was denied a second touchdown for a forward pass, but Goodwin on the other flank did get over after being set up by Marshall.
Goodwin then capped off his test debut with a second try with three minutes to go.
New Zealand now head to the Four Nations, which they open with a test against Australia in London on October 25 (NZ time).
Tonga are bound for the Pacific Cup in Papua New Guinea beginning on October 24.
The cup winners will qualify as the fourth team, replacing France, in next year's Four Nations, which will be held in the Southern Hemisphere.