Matty Russell scored a late try as Scotland snatched a dramatic World Cup win over Tonga, who looked to have pulled off a stunning comeback.
Glen Fisi'iahi scored an early try for Tonga but Scotland dominated the opening half, touching down three times to lead 20-4 at the break.
The Tongans responded with four unanswered tries after the restart as they moved into a 24-20 lead.
But Russell powered his way over for a try that Danny Brough converted.
It was a thrilling end to a physical and engrossing encounter that Scotland looked to have under control at the break before their opponents stole the momentum with some blistering attacking rugby.
Tonga scored five tries to Scotland's four but the trusty boot of Huddersfield Giants' Man of Steel Brough ultimately proved decisive.
The Bravehearts half-back converted all four tries and kicked a penalty, while Tonga's Samsoni Langi managed only two conversions.
Chilly and at times soggy conditions at Derwent Park failed to dampen the spirits of the crowd of 7,630 or the two teams in a thrilling match.
It took just nine minutes for Fisi'iahi to give Tonga the lead, crashing through a gap left by Kane Linnett, but Scotland responded brilliantly.
Inspired by half-backs Brough and Peter Wallace, Steve McCormack's side went ahead when Russell scored the first of his two tries, stepping and powering his way through Tongan tacklers.
Brough clipped his penalty through the posts to extend the lead, then Ben Fisher made the most of Fisi'iahi's inability to gather a grubber kick to touchdown.
It got better for Scotland when Brett Carter slipped in unopposed on the right to score on his home ground, but it owed much to an offload from Danny Addy.
The Pacific Islanders headed for the sheds looking pretty shellshocked, but whatever was said seemed to spur them on as they ripped into the Scots after the break.
Sika Manu set the ball rolling, capitalising on a Scotland error to ground the ball, and video referee Ashley Klein awarded the try in the first of a series of calls sent his way from match official Shayne Hayne.
Indiscipline from Scotland gave Tonga a penalty and, from the resulting set, Nafe Seluini forced his way in on the back of brutal attack from Fuifui Moimoi. After two no-try calls from Klein, Sika Manu's faintest of dab downs was awarded.
Willie Manu joined his namesake on the scoresheet when he was put in by Daniel Tupou as Tonga took the lead, before Russell set up a thrilling finale with his try, converted by Brough with eight minutes to go.
Tonga poured forward and could have levelled if they had kicked a late penalty, but Scotland held out and begin their campaign with success.
Scotland are next in action on Sunday when they take on Group C leaders Italy, while Tonga play Cook Islands on 5 November.
Scotland coach Steve McCormack: "Many people didn't give us a chance looking at that Tonga team, who showed glimpses, but we found a way to win.
"That is up there with the best Scotland have ever had.
"We earned everything we got. Tonga were brilliant for 30 minutes, but we found a way to win.
"In the last 10 we held firm. My heart was in my mouth and I was not sitting comfortably, but we found a way to win. I am very proud to be associated with that."
Tonga coach Charlie Tonga: "This is a bit of a wake-up call.
"We now have to regroup and ask the boys to go out and do their best for the rest of the tournament."
Tonga: Fisi'iahi, Tupou, Hurrell, Taufua, Feki, Langi, Foster, Kite, Havili, Moimoi, Taumalolo, S. Manu, W. Manu, Murdoch-Masila.
Replacements: Seluini for Havili (23), Murdoch-Masila for Moimoi (19), Ta'ai for W. Manu (36), Paea for Murdoch-Masila (20).
Scotland: Russell, Hurst, Hellewell, Linnett, Carter, Brough, Wallace, Walker, I. Henderson, Douglas, Addy, Ferguson, Wilkes.
Replacements: Stringer for Walker (26), B. Phillips for I. Henderson (67), Fisher for Douglas (32), Kavanagh for Wilkes (19).
Ref: Shayne Haynes (Australia).