Scotland (4) 16
Scotland were the ones celebrating their World Cup qualification
Tries: Fisher, Benn
Goals: Brough (4)
Wales (14) 18
Tries: Dyer, Hughes, Gibson
Goals: Harris (3)
Scotland claimed the ninth place in the 2008 World Cup in Australia despite losing to Wales in Glasgow.
Damien Gibson's 78th-minute try for the Welsh was not enough to overturn a seven-point deficit from the first game in Bridgend.
It means the Scots join England and Ireland in next year's finals thanks to an aggregate score of 37-32.
The Welsh must now win the repechage if they are to claim the 10th and final place in Australia.
Martin Hall's team take on Lebanon in Widnes on Friday night, with Samoa or United States awaiting the winners at Featherstone on Wednesday week.
The Scots, who won 21-14 in Bridgend, trailed 14-4 at half-time but scored second-half tries through hooker Ben Fisher and winger Jamie Benn to keep themselves in contention.
Captain Danny Brough crucially kicked four goals from four attempts.
Wales, directed by experienced half-backs Iestyn Harris and Lee Briers, had the edge in the first half, but the never-say-die Scots had the final say.
Former Hull KR centre Luke Dyer took Phil Joseph's pass to open the scoring with an eighth-minute try and Harris kicked the first of three goals.
Brough kept his side in front with two first-half penalty goals and full-back Wade Liddell had a try disallowed after video referee Ben Thaler ruled that he failed to control the touchdown.
Ben Fisher was among Scotland's try scorers in Glasgow
The Scots also suffered a blow when loose forward Richard Fletcher was carried off on a stretcher after being hurt in an aerial collision that caused a six-minute stoppage.
It was during the added time that Wales scored their second try, centre Adam Hughes taking substitute Andy Bracek's pass to force his way to the line.
Wales winger Anthony Blackwood was twice denied tries and the misses began to look costly when Scotland scored their first try 12 minutes into the second half to edge his side back in front on aggregate if not on the scoreboard.
Brough kicked his third goal to provide a small cushion, but it needed a desperate last-ditch tackle from centre Mick Nanyn to deny Harris after he had gathered Briers' kick.
In a nailbiting finish, Scotland winger Jamie Benn had a try disallowed for an obstruction by Nanyn - and Harris went within inches of scoring at the other end.
The pivotal moment came after 73 minutes, when Nanyn superbly kept the ball alive and Benn successfully chased Neil Lowe's speculative kick-ahead for the vital score.
Wales had the final say when Gibson went over for a third try that gave them only their second win in five meetings with the Scots, but it was a hollow success.
Scotland: Liddell, Benn, K. Henderson, Nanyn, Todd, Patterson, Brough, Jackson, Fisher, Wilkes, MacGillivray, Morrison, Fletcher.
Substitutes: Lynn, Marsh, Lowe, A. Henderson.
Wales: Halley, Blackwood, Dyer, Hughes, Gibson, Harris, Briers, Cushion, Webster, Dean, M. James, Roberts, Joseph.
Substitutes: Penkywicz, J. James, Kopczak, Bracek.
Ref: P Bentham (Warrington).