But their hopes were wrecked when Zander Fagerson was sent off for a dangerous clear-out and Wales ruthlessly exploited their man advantage.
Gloucester wing Rees-Zammit had scored just before half-time to keep Wales in the contest, with his second – after Liam Williams and Wyn Jones had also crossed over – confirming a bonus point.
Hogg did briefly put Scotland back in front, but Pivac’s team held their nerve to claim their first win on foreign soil in five attempts under Warren Gatland’s replacement.
There was no repeat of last week’s Twickenham knee-gate row as all 23 Scotland players plus the visitors remained standing with heads bowed during the ‘moment of reflection’ called for by the tournament organisers to show support for the fight against racism.
Some remarkable discipline saw the Scots pinged just six times against the Auld Enemy, but they were not quite so tight this time, handing over three penalties inside the opening seven minutes.
The Scotland stand-off was one of the chief architects of that Twickenham triumph and he was flying by the seat of his pants again this time, chucking around passes so flat they missed Welsh noses by mere millimetres.
And it was from his daring offload to Jonny Gray that the opening try came after 19 minutes. The big lock did not have the speed to go all the way, but Scotland recycled possession before Ali Price’s sublime chip over the static Welsh defence dropped into Graham’s hands and he scampered round Halfpenny to dot down under the posts.
It was a despairing effort from the Welsh full-back but he should have done much better six minutes later.
Hogg started and finished the move that brought the second as he chipped over the back of the visitors.
The ball should have been easily gathered by Halfpenny, but instead he let it tumble from his grasp as his opposite number appeared in a flash to scoop the ball up and slide over the line to put his team 17-3 up on seemingly on course for victory.
Halfpenny ended up going off to have a head knock assessed after a mid-air clattering with Graham, who was looking a threat every time he got his hands on the ball.
His namesake, Scotland replacement Gary Graham, who had taken over from the concussed Blade Thomson early, was also proving a menace – to his own side.
First he wrecked a good scoring chance by getting caught on top of the ball at a ruck, before giving Wales good territory with a high tackle on Taulupe Faletau.
It was a costly mistake as Wales got the momentum changer they desperately needed 90 seconds before the break. Tying the Scotland pack up at the breakdown, the visitors worked the ball wide through Tompkins and Williams before Rees-Zammit ran in for the touchdown.
Scotland hoped that was just a blip. It may have been had Scott Cummings not denied Gary Graham the chance to make amends after 48 minutes as he was ruled to have obstructed the Wales defence as Darcy Graham charged over the line.
And instead of going two scores in front, Townsend’s team found their lead trimmed back to two points a minute later as the Welsh pack careered forward from a devastating line-out maul before Rees-Zammit released Williams in space to score.
Things went from bad to worse for the hosts after 53 minutes as Fagerson got his marching orders for slamming into Jones with an illegal clear-out.
Against 14 men, Pivac’s team needed just 90 seconds to nudge in front as Jones barged over from another well-executed line-out manoeuvre.
Scotland still had plenty of time to respond but they needed someone to take command.
Up stepped Hogg as he collected a Russell pass before setting off on a 15-metre rampage towards the corner as he brushed off Owen Watkin and Tompkins before getting the ball down on the line.
But Wales simply had too much space to play with and they claimed the win with 10 minutes left as Rees-Zammit chipped the ball over Hogg’s head before racing through to claim the bonus point.
Scotland fought to the end but their hopes were dashed in stoppage time as Hogg failed to hold onto Duhan Van Der Merwe’s desperate last charge.