Warren Gatland's team recovered from conceding two tries in the opening 10 minutes to wing Josua Tuisova and full-back Kini Murimurivalu as Fiji showcased their sevens genius in spectacular fashion.
But wing Josh Adams touched down twice before half-time, and then completed his hat-trick to secure a potential last-eight clash against France.
Dan Biggar, who went off after a nasty collision, kicked two conversions and Rhys Patchell added a conversion and penalty, but Wales were pushed to the limit before full-back Liam Williams' late try clinched a bonus-point triumph.
Fiji's defeat means they are out of the tournament, yet they exited in style despite having two players sin-binned, by testing every sinew of Wales' character and resolve.
Wales showed two changes in their line-up, with flanker James Davies and number eight Ross Moriarty both making their first starts of the tournament.
Edinburgh back-row forward Viliame Mata, meanwhile, featured as Fiji's solitary switch following a bonus-point victory over Georgia last week.
Fiji served immediate notice of their attacking flair when lock Leone Nakarawa's defence-splitting pass had Wales in trouble.
And Wales were then undone from an attacking scrum, with possession quickly moved and wing Tuisova applying a one-handed finish for a superb try.
Flanker Josh Navidi looked to have hauled Wales level two minutes later when he crossed Fiji's line, but the score was ruled out for a knock-on by centre Hadleigh Parkes.
Fiji continued to stretch their opponents, though, and scrum-half Frank Lomani saw a try disallowed before Wales hooker Ken Owens was sin-binned.
Gatland's side were all over the place and they conceded a second try when Murimurivalu touched down in the corner.
The Six Nations champions did not know what had hit them as Fiji began to evoke memories of their stunning World Cup victory over Wales in Nantes 12 years ago.
Fiji lock Tevita Cavubati followed Owens into the sin-bin for an offence at a ruck, and Wales responded with an opening try after Adams caught Biggar's kick to touch down, with the latter converting.
Wales regrouped impressively and Fiji had a second player sin-binned when flanker Semi Kunatani saw yellow for deliberate offside.
Relentless pressure had to tell and Fiji could not hold out as Adams touched down again, before Biggar's conversion made it 14-10 at the interval.
Fiji had no intention of going quietly, with Wales stretched defensively during the second half's opening minutes.
Wales captain Alun Wyn Jones' handling error under no pressure in midfield illustrated how much of an impact Fiji had made on the game, and Davies was then sin-binned for killing possession.
Davies had hardly made his way off the pitch when the Wales forwards pulled down a Fiji driving maul and referee Jerome Garces awarded a penalty try that meant Wales trailed 17-14 after 54 minutes.
Patchell replaced Biggar and he immediately kicked a long-range penalty that tied the scoreline.
It was a thrilling and pulsating contest, with Wales hoping their fitness levels might prove decisive as the game entered its closing quarter.
And when centre Jonathan Davies made a slashing break, Adams finished brilliantly to complete his hat-trick, giving Wales a five-point advantage.
Williams' touchdown sealed the deal for Wales, prevailing following a memorable encounter that saw them repeatedly dig deep to emerge victorious.