The Scots put in a gutsy display after crashing embarrassingly to Ireland in their Pool A opener last weekend.
That defeat, combined with Japan's shock win over Joe Schmidt's men, left the Scots staring at an early exit.
Sean Maitland and Grieg Laidlaw both grabbed first-half touchdowns before referee Pascal Gauzere issued two penalty tries after the break to seal a much-needed bonus point. Samoa winger Ed Fidow got a red card for giving away both of them.
And a delighted Gregor Townsend said afterwards: "That was a true reflection of who we are and what we are capable of and what playing for Scotland means to the players.
"That was a tough challenge to face, knowing that if we underperformed we were out of the World Cup. To see the effort and togetherness was excellent.
"Last week we missed the beginning of the game and I take responsibility for not preparing the team well enough to start well against Ireland.
"But that didn't make us a bad team. Tonight was great to see them get rewards for their effort."