Samoa, frontrunners after beating Fiji and Cook Islands in earlier group matches, lose out on automatic qualification because of an inferior points difference but could win a reprieve in the repechage.
The Samoans would have gone through had they lost by six points and the outcome rested on a missed goalkick from full-back Tangi Ropati, arguably their best player.
Man of the match was Harlequins RL forward Solomon Haumono, who led the Tongans from the front, scoring the all-important try on the stroke of half-time that broke the deadlock.
Tonga substitute Taniela Tuiaki had opened the scoring in the 22nd minute but Conrad Ta?akimoeaka pulled Samoa level four minutes before half-time after Castleford Tigers second rower Gray Viane had made the crucial break.
Winger Malo Somomona, cousin of New Zealand Test forward David, added a second try for Samoa early in the second half but the match was settled when substitute Makasini Richter went over on the hour and scrum-half Joel Taufa?ao kicked his third goal from as many attempts.