Rudi Brown's try and Ben Afshar's penalty set the hosts on their way before the break, before Wales hit back through Morgan Morse's touchdown just after the break.
A penalty try put Scotland back in control of the contest, but Wales took the lead thanks to a Dan Edwards penalty and a score from Oli Andrew.
But Scotland, who previously hadn't won in this competition for three years, weren't to be denied, and sealed the win when Dan King slotted over a penalty late on.
The visitor paid a big price for their lack of discipline with Wales conceding four yellow cards over the course of the match.