Scotland (12) 18
Tries: Steel, Robertson, Wilkes Goals: Brough 3
Fiji (4) 16
Tries: Tadulala 2, Bukuya Goals: Naiqama 2
Cannot play media. Sorry, this media is not available in your territory.
Highlights - Scotland 18-16 Fiji
Danny Brough's conversion of Oliver Wilkes' late try allowed Scotland to pip heavily fancied Fiji and claim their first ever World Cup win.
But it was the margin of victory was not enough to put Scotland into the semi-final qualifier against Ireland.
Winger Semi Tadulala looked to have given Fiji the win with his second try eight minutes from time.
But Steve McCormack's side refused to give up and Wilkes went over three minutes later to secure a famous win.
Scotland needed a win by 28 points or more to top the group and reach the semi-final qualifier.
And while that was never on the cards against an accomplished Fijian side, Scotland played with real pride to ensure they finished second in the table, setting up a ranking play-off match against Tonga.
Fiji almost took the lead within the first minute through full-back Jarryd Hayne but it was Scotland who scored the opening try when winger Jon Steel took advantage of smart work from Henderson down the blindside to step inside and touch down.
Brough added the conversion from the touchline for an early six-point advantage.
Fiji hit back when a great run by Hayne, followed an excellent ball from Sevanaia Koroi put Tadulala over in the left-hand corner.
A confrontation between Ian Henderson and Jason Bukuya at the play-the-ball quickly turned into a boxing match with punches flying in from both players before the referee managed to regain control.
A fired-up Scotland hit the front again on half-time when Brough's hopeful kick struck the upright and Manly full-back Michael Robertson reacted the quickest to just touch down before the ball rolled dead.
Brough's conversion gave his side an eight-point interval lead but the Fijians came back into the game after the break and were soon level.
Bukuya forced the ball down despite being surrounded by four Scottish defenders with Wes Naiqama converting from the right-hand touchline.
And Naiqama then added a penalty eight minutes later after Scotland's markers were caught not square.
Both sides then had the opportunity to push ahead, with Scotland having four consecutive sets on the opposition line, and Fiji's Jone Macilai seeing a try disallowed when a team-mate strayed offside.
But after a period of stalemate, Nick Bradley-Qalilawa helped create Tadulala's second try to edge the Fijians in front.
That looked to have won it until Wilkes and Brough's dramatic late contributions.
Scotland: Robertson, Liddell, Nanyn, K. Henderson, Steel, Duffy, Brough, Wilkes, Fisher, Logan, Morrison, Armit, I. Henderson.
Replacements: A. Henderson, Morton, Jackson, Lowe.
Fiji: Hayne, Tadulala, Naiqama, Koroi, Uate, Bradley-Qalilawa, Groom, Divavesi, Sukanaveita, Vonomateiratu, Sims, Tora, Bukuya.
Replacements: Sadrau, Wesele, Macilai, Nauqe.
Referee: Leon Williamson (New Zealand)