Telusa Veainu and Jack Ram crossed for Tonga, but Namibia's Johan Tromp pulled a try back, before Latiume Fosita's try helped Tonga into a 22-7 lead.
Ram got his second score soon after the break to secure Tonga's bonus point, although Namibia captain Jacques Burger was then forced over to keep it close.
But Veainu's second try secured Tonga's first win of this World Cup, despite a late consolation score from Burger.
Following a shock 17-10 loss to Georgia in their opening game, the win lifts Tonga to second place in Pool C, although their meeting with Argentina on Sunday is a must-win if they are to have any hope of progressing to the quarter-finals.
A first ever World Cup win still eludes Namibia, but they scored three tries in a match at the tournament for the first time.
n front of a capacity crowd at Exeter's Sandy Park, it was clear that neither side wanted a narrow, forward-orientated game, and the outcome was a thrilling clash.
Namibia's Danie van Wyk got the crowd going with a dangerous run before Tonga hit back a minute later with the first try.
Veainu broke from midfield and surged through the Namibian defence before going over despite some desperate last-ditch defending.
Flanker Ram showed a great turn of pace six minutes later as he skipped through the Namibian line to score after breaking down the blindside.
But the African underdogs ensured they would not go down without a fight as Tromp got his first try of the World Cup following an excellent run through Tonga's defence by second-row Tjiuee Uanivi.
A try from Fosita and a penalty from Lilo increased the lead before Veainu had a try chalked off by the TMO after he dived over in the corner, having just put a foot into touch.
Tonga might have had the game wrapped up at half-time, but inspirational Namibia skipper Burger ensured they would not go down without a fight.
After Ram had extended Tonga's lead soon after the break with his second try, the Saracens flanker was forced over the line by his Namibian pack to score the first of two tries.
Namibia changed their attacking impetus, trying to play the game more through the forwards, and to an extent it worked, but Tonga looked dangerous whenever they had ball in hand.
Veainu illustrated that danger as he got Tonga's final try with 26 minutes left, going over in the left corner.
But the inspirational Burger went over for his second try after another strong maul from the Africans. Kurt Morath added a late penalty for Tonga, becoming his country's all-time record points scorer in the process.
As Tonga face a potentially decisive clash with Argentina, Namibia will look to next Wednesday's clash with Georgia as their best chance to break their 17-game winless run at World Cups, stretching back to 1999.