Scotland stunned Wales with a late salvo to sink any hopes Jason Strange's side had of making the fifth-place play-off final with a shock 29-25 victory in the World Rugby U20 Championship.
Wales had beaten Scotland twice this season and began the match as slight favourites but Scotland deserved victory having outscored Wales 4-3 on try count in a thrust and counter thrust encounter.
Strange's side entered the middle-tier of the competition following their eight-try victory over Samoa, with fifth-place the highest finishing position up for grabs.
Half backs Dane Blacker and Arwel Robson returned to the starting line-up, as did Will Talbot-Davies and Ryan Conbeer. In the pack, Ellis Shipp returned at hooker and Will Griffiths, who sat four A-level exams last week, was back in the second row.
Scotland head coach John Dalziel, on the other hand, kept faith with the side that defeated Italy in their previous game, by naming an unchanged line-up.
Wales dominated the early exchanges but couldn't make their territory ascendency pay until the 13th minute but it was worth the wait.
Centre Ioan Nicholas intercepted a promising Scottish move and then combined to put midfield partner Owen Lane clear. Lane was cut down 10m short of the line but Nicholas was on hand to take his flip pass and send captain Will Jones racing over without a hand being laid on him.
Scotland hit back with left wing Darcy Graham cruising over out wide after Wales' defence was caught napping, fly half Connor Eastgate's conversion edged Scotland 7-5 ahead.
Sloppy work from a defensive lineout ultimately led to centre Ross McCann crashing over to make Wales pay for their benevolence, Eastgate adding the extras to increase Scotland's lead.
Wales hit back almost immediately with fly half Arwel Robson slashing through the Scottish rear guard to send Nicholas over under the posts. Robson's conversion brought Wales to within two points of Scotland.
A defensive line-out proved Wales downfall again, as they were unable to halt the Scottish pack driving over captain Callum Hunter-Hill in what was turning out to be a try-fest.
Robson pegged back three points in the 51st minute as the game became niggly. The staccato nature continued when Eastgate cancelled out Robson's penalty and then the Wales fly half responded with his second success of the second half from close range.
Wales regained the lead with 10 minutes remaining when Robson wriggled his way over near the corner flag despite Fagerson doing his utmost to drag him into touch. Robson's excellent conversion gave Wales a three point cushion.
With five minutes remaining Graham picked up a loose ball and smouldered through the defensive line to score under the posts. Eastgate's boot added the extras and Scotland had five minutes to hold on to the lead.
The fact every Scottish player jumped in the air with delight at the final whistle showed what it meant to beat Wales, who will now face Italy in the play-off final for seventh place.