Aided by a strong tailwind, Canada took a 10-0 lead after 26 minutes and seemed to have the measure of the bigger Tongan lineup, but conceded a try on the stroke of halftime to surrender the ascendancy.
Centre Siale Piutau crossed twice as Tonga scored 20 of the next 23 points before Canada responded again with two late tries running into the wind, both resulting from Tongan turnovers, and defending grimly for the last seven minutes -- slightly delayed by a streaker running into the field and diving under the goalposts.
"We were obviously under a lot of pressure ... but the guys stuck to our systems and trusted our defence," MacKenzie said. "We got the ball back and put them under pressure and I got lucky I guess.
"At that point, I didn't really care who scored it as long as it was us."
Canada coach Kieran Crowley said he was proud of the scrum, which more than held its own against the Tongan set-piece, and the committed defence.
"Two or three years ago we would have lost that," he said.
France-based lock Jamie Cudmore agreed composure was the difference in the vital Pool A game.
"In the past, that might have been a downfall of ours," he said, "when we got five, seven points behind ... things start snowballing the wrong way pretty quick.
"This time, we all ... made sure we kept everybody calm, kept it precise and made sure we'd stick to our gameplan."
Canada has now beaten Tonga in all three World Cup head-to-heads, dating back to the inaugural tournament here in 1987 when Crowley was a member of the winning All Blacks squad.
"That was awesome. I'm so proud of the boys, the way they played," Canada captain Pat Riordan said, "Just awesome."
Canada plays two-time finalist France on Sunday, a short turnaround which Crowley says disadvantages the tier two nations because all the tier one countries receive a full week between matches.
Tonga has already experienced that, losing for the second time in five days after a 41-10 defeat to No. 1-ranked New Zealand in the tournament's opening match.
"Very, very disappointed," Tonga coach Isitolo Maka said. "We made silly mistakes and it cost us the game."
Maka said he'd sent out instructions after the second try to use the breeze and kick for field position, keeping the ball deep in Canada's territory.
"And we tried to run the ball from our half -- and then that's where they scored from," he said.
Despite the loss, which just about ends Tonga's chances of advancing in the top two of a group also containing New Zealand, France and Japan, Maka defended his decision to rotate 11 new players into the starting lineup after the loss to New Zealand, saying it was only poor decision-making which cost his team the win.
Bearded flanker Adam Kleeberger was voted man-of-the-match for his tireless work containing the big Tongans at the breakdown.
"They did put a lot of pressure on us at the end, but our defence held up," he said. "We've had a belief in the side that we can take on any side. And when we came back at the end there, I think we proved that to ourselves."
Local organizers ran a promotion to "Paint the Town Red," and the citizens and visiting fans obliged, with Northland Events Centre packed with more than 17,000 fans, mostly wearing the colours and flying the flags of both national teams.
The Tongans went out with clear intent to run the ball, with flyhalf Taniela Moa spreading the ball wide at every opportunity. But they were let down by basic handling errors and a high-risk approach in the gusty conditions that eventually backfired.
The Canadians capitalized in the 13th minute when lock Jebb Sinclair crashed over in the right corner after centre DTH van der Merwe's bumping run to finish off a movement that started behind halfway.
Fullback James Pritchard landed the conversion and a penalty to make it 10-0, but the Tongans hit back after relentlessly grinding away through the forwards on the left side before Moa threw a long, looping cutout pass out for Piutau.
Kurt Morath, who missed his first two shots at goal, landed a more difficult conversion to make it 10-7 at the break.
Piutua combined again with Moa in the 53rd, charging onto a neat inside hand-off on the quarterline to cross untouched for his second try.
That gave Tonga the lead for the first time, and Morath extended the buffer to 20-13 with a conversion and penalty before the Canadian comeback.
No. 8 Aaron Carpenter carried four defenders over the line with him in the 67th after charging runs from Van der Merwe and substitute Conor Trainor. Pritchard missed the conversion attempt which would have levelled the scores, but Canada maintained the attack, converting pressure into points when flyhalf Ander Monro found space with a solo dart inside the attacking quarter and then MacKenzie crossed in the next phase.
Tonga has a week before it takes on Japan.
Canada 25 (Jebb Sinclair, Aaron Carpenter, Phil MacKenzie tries; James Pritchard 2 conversions, 2 penalties) def. Tonga 20 (Siale Piutau 2 tries; Kurt Morath 2 conversions, 2 penalties.) HT: 10-7.