Wales bade farewell to 100-cap Martyn Williams by killing off Shane Williams' hopes of a winning send-off for the Barbarians at the Millennium Stadium.
Wales went into a 13-0 lead through Harry Robinson's try and James Hook's kicks, but the visitors led 14-13 at the break after tries by Stephen Donald and Richie Rees.
The Baa-Baas went further ahead when Donald went over for his second.
But Hook and Aled Brew tries sealed Wales' victory.
Shane Williams led out the Barbarians for his final Millennium Stadium appearance hoping for the chance to produce some of the magic with which he has graced the game.
Robinson scores brilliant debut try
There was also the significant matter of a 100th cap for another Welsh Williams - flanker Martyn - who avoided what he described as the "pain" of being left stranded on 99 caps.
Martyn Williams came on as a 46th-minute replacement and one of his first acts was to pull down wing Shane as the Baa-Baas wing threatened to break free.
That moment was undoubtedly among the post-match talking points.
But while Welsh fans mulled the glorious past of the Williams duo, a new rising star was also on their lips.
Wing Robinson, 18, scored the opening try on his debut after being set free by a combination of Lloyd Williams, Ian Evans and Matthew Rees.
The Cardiff Blues wing's pace ensured there was no chance of the visitors running him down on a burst from half-way that had the crowd on its feet..
Hook had kicked an early penalty and converted Robinson's score before completing a 13-0 cushion in the 26th minute with a second penalty.
But when Australian lock Matt Chisholm burst past Robinson, the invitation club were on their way to a try-from-nothing that set up a telling fight-back.
Chisholm sent Donald over, the World Cup-winning New Zealand fly-half also adding the conversion.
The hosts were in further trouble after scrum-half Lloyd Williams was charged down in the shadow of his posts.
Under pressure in defence, prop Rhys Gill was sin-binned for illegally killing the ball.
Video referee Nigel Whitehouse twice told referee Alain Rolland that the Barbarians had failed to score, Scottish number eight Johnnie Beattie and Georgian back-row colleague Mamuka Gorgodze the men denied.
But at the next time of asking erstwhile Wales scrum-half Rees' score was given after he latched on to a loose ball at a ruck.
Donald converted to give the Baa-Baas a one-point lead at the break.
Flanker Williams came on to a warm reception to reach his 100 caps and showed his guile by thwarting Shane Williams' threat.
However, the visitors were not to be denied when Donald went over from close range again, leaving the hosts with it all to do.
Rhys Webb made that task easier by sending out a pass that sent Hook into space and the Perpignan player did the rest on a mazy 35-metre run before converting.
And Shane Williams also played a part in Wales' win' albeit in unintentional fashion.
The retired Osprey launched a counter-attack only to see the man wearing 11 on this occasion, Brew, intercept and run clear for the final try, Hook again coverting.
Liam Williams (Scarlets); Harry Robinson (Blues), Andrew Bishop (Ospreys), James Hook (Perpignan), Aled Brew (Dragons); Dan Biggar (Ospreys), Lloyd Williams (Blues); Rhys Gill (Saracens), Matthew Rees (Scarlets, capt), Rhodri Jones (Scarlets), Alun Wyn Jones (Ospreys), Ian Evans (Ospreys), Josh Turnbull (Scarlets), Justin Tipuric (Ospreys), Ryan Jones (Ospreys).
Richard Hibbard (Ospreys, for Rees, 68), Paul James (Ospreys, for Gill, ), Aaron Shingler (Scarlets, for Ryan Jones, 51), Martyn Williams (Blues, for Alun Wyn Jones, 46), Rhys Webb (Ospreys, for Lloyd Williams, 51), Adam Warren (Scarlets, for Biggar, 64), Will Harries (Dragons, for Robinson, 68).
Not used: Paul James.
Mils Muliaina; Isa Nacewa, Casey Laulala, Mike Tindall, Shane Williams; Stephen Donald, Richie Rees; Duncan Jones, Benoit August, John Smit (capt), Mick O'Driscoll, Matt Chisholm, Francois Louw, Johnnie Beattie, Mamuka Gorgodze.
Cedric Heymans for Muliaina (66), Sailosi Tagicakibau for Laulala (46), Aled de Malmanche for August (40), Neemia Tialata for Smit (46), Anton van Zyl for O'Driscoll (40).
This page is best viewed in an up-to-date web browser with style sheets (CSS) enabled. While you will be able to view the content of this page in your current browser, you will not be able to get the full visual experience. Please consider upgrading your browser software or enabling style sheets (CSS) if you are able to do so.