Scotland were unable to secure back-to-back Six Nations wins as a red card hampered their hopes at Murrayfield.
Wales prevailed by a single point after Zander Fagerson was sent off early in the second half for a dangerous ruck clear out.
The Scots had held a commanding lead but saw it slip as ill-discipline dogged their efforts to go top of the championship table.
The visitors were first to tick the scoreboard when some patient phase play in Scotland territory milked an offside penalty and Leigh Halfpenny kicked three points from the tee.
The Scots were quickly level when Finn Russell kicked a penalty after Alun Wyn Jones was penalised at the breakdown.
Wales had started the brighter but Scotland soon found their foothold and crossed for the first try in the 19th minute.
Jonny Gray broke the defensive line and two phases later a chip from Ali Price found the grateful arms of Darcy Graham who walked over to score.
The men in dark blue had found their groove and a clinical set play off a scrum saw captain Hogg chase his own grubber through to slide over the whitewash.
Wales were on the ropes but a five minute spell of indiscipline from the Scots allowed them back in the game before half time.
A succession of penalties allowed Wayne Pivac’s side to piggyback down the field and, on another penalty advantage, Louis Rees-Zammit dived over on the right wing to reduce the lead to nine points at the break.
The turnaround saw the game see-saw and after Scotland turned down a kick for three points only to come away from the Welsh line empty handed, the visitors powered right back into the contest when a rolling maul set them up to send Liam Williams over for a try on the left.
With the lead reduced from 14 to just two points Scotland were put deep in the mire as Zanderson was given a red card for a clear out that connected with Wyn Jones’s head.
Seconds later Jones has muscled over and Wales led the match for the first time since the eighth minute.
The contest was set to match last week’s Calcutta Cup for drama now. Scotland were still turning down kicks at goal but finally they were vindicated when, off the back of a set scrum, Hogg managed to accelerate off the soft shoulder of a defender to score his second try of the contest.
Russell converted from the touchline to establish a four point lead again with 13 minutes to see out with 14 men.
With Darcy Graham sacrificed, Wales knew the numerical advantage would be felt on the wing and they quickly had their noses ahead again through Rees-Zammit who chipped over Hogg and latched on to the ball to score and edge the men in red ahead again.
Scotland held the lion’s share of possession for the final ten minutes but found the Welsh defence too tough to breach.
A last gasp break by Van Der Merwe was halted by an incredible tap tackle by Owen Watkin and Wales were able to smuggle the ball out to herald the final whistle.
Scotland Player Ratings
Stuart Hogg 9 – Did everything he could to drag his team to victory, including two brilliantly taken tries.
Darcy Graham 8 – Gave the defence nightmares with his jinking runs and kick chases.
Chris Harris 8 – Quiet in attack last week but at the heart of Scotland’s running this time round as he carried hard in Welsh territory.
James Lang 6 – A quiet afternoon without any major contributions.
Duhan van der Merwe 7 – Carried hard and made metres after contact but never got himself free out in open field.
Finn Russell 7 – Picked up where he left off at Twickenham with a game of varied distribution and no errors.
Ali Price 7 – Led the side around the field well and made some great positional kicks in the first half.
Rory Sutherland 7 – Held up his side of the scrum and made some powerful carries.
George Turner 6 – Could not sustain his perfect throwing record and a rash clear-out ended teammate Thomson’s game.
Zander Fagerson 4 – To-and-fro with Wyn Jones ended with a red card for the prop for a dangerous clear-out.
Scott Cummings 6 – Mix up on the line wrecked chance to stretch Scotland’s lead.
Jonny Gray 7 – Trusted to make some big calls which paid off with Hogg’s second try.
Blade Thomson 5 – Made one big carry then forced off with a head knock with less than 15 minutes played.
Hamish Watson 7 – Got around and played with power and menace at the breakdown.
Matt Fagerson 8 – Carried with power and menace and solid in defence. Another huge step forward on the test stage.
Replacements 6 – Helped Scotland keep possession in late stages but unable to stage a late comeback.