Australia spoiled the occasion by winning, as they ran in three tries following Leigh Halfpenny's second half sin-binning.
Will Genia, Lachie Turner and Berrick Barnes exploited the space to cross in quick succession.
But Rhys Priestland went over for Wales before Williams signed off in typical style.
Wales had hoped to gain some revenge for their World Cup bronze medal defeat to the Wallabies last October.
But Warren Gatland's side were without six players unavailable at French and English clubs, while injury claimed key forwards Adam Jones, Alun Wyn Jones and Luke Charteris, plus centre Jon Davies.
Australia in contrast were almost at full strength, but continued with the experiment of playing goal-kicking wing James O'Connor at fly-half, after a successful outing against the Barbarians a week earlier.
Williams choked back tears during the Welsh anthem in a stadium that was already full of emotion after a minute's applause for the late Wales football manager Gary Speed.
Priestland drew first blood for Wales with a 40m kick, after Wallabies captain James Horwill infringed.
Tearful Williams says thank you to Wales fans
Priestland blotted his copy-book soon after when the fly-half's attempted clearance was charged down, only for Australia to go offside in their eagerness to get over the try line.
After 12 minutes the crowd got what they had been baying for as the ball reached Williams on the left wing, only for the man of the moment to knock-on as he eyed the space in front of him.
O'Connor missed what for him was a regulation penalty, as his kick just faded across the front of the posts, but the visitors then almost took the lead.
Barnes spotted space on his right and dinked a pin-point chip over the shoulder of wing Williams.
Turner gathered and pelted for the corner, but Williams did superbly to get back and just drag his rival into touch at the corner.
When Australia tried to breach Wales head-on, their defence showed it had picked up from where it left off in the World Cup.
Wallaby players were regularly knocked back in the tackle, including one eye-catching hit by Jamie Roberts on Barnes, although the notable exception was Digby Ioane who came off his wing to great effect.
Wales pounded upfield, helped by charging lock Ian Evans who was back in a Wales shirt after a three-year absence, and Priestland took advantage of another offside to kick his second penalty.
But after Wales conceded a penalty as they struggled to contain a counter-attack, O'Connor made no mistake to pass 200 Test points and reduce Wales' lead to 6-3 at the break.
Australia took control and the lead soon after the restart when Wales had Halfpenny sin-binned.
O'Connor showed all his winger's pace to chase a kick through and the Wales full-back tackled the charging Aussie just before he gathered the ball.
Halfpenny's intervention probably saved a try but referee Jonathan Kaplan decided a yellow card and a penalty were sufficient punishment.
Gatland focused on youth as Williams retires
Australia opted to take a quick tap and stretched Wales before scrum-half Genia sniped over for a try which O'Connor converted.
O'Connor then missed a penalty from in front of the sticks, his kick cannoning back off the left-hand post.
But Australia kept up their stranglehold to send wing Turner over on the left, with O'Connor finding his kicking range from tight on the touchline to give his side a 17-6 lead.
A third try soon followed as replacement Radike Samo - on for open-side David Pocock - delayed his pass to send Barnes over.
Wales, now back up to full strength and with hooker Matthew Rees off the bench as well, hit back almost immediately.
Giant young winger George North again punched some hard yards and the ball was recycled quickly for Priestland to go over, although the fly-half mirrored O'Connor by hitting the post with his conversion.
There was still time for Williams to produce some dazzle to remind the game's fans of what will be missing from the Test arena.
And the fairytale try arrived for the 34-year-old with the last attack of the game, as Williams popped up in midfield to skip past Barnes and go over with a somersault flourish.
Dan Biggar's conversion still meant Australia had won 24-18, but the crowd hardly cared as Williams was treated to a standing ovation at the final whistle.
Leigh Halfpenny (Cardiff Blues); George North (Scarlets), Scott Williams (Scarlets), Jamie Roberts (Cardiff Blues), Shane Williams (Ospreys); Rhys Priestland (Scarlets), Lloyd Williams (Blues); Gethin Jenkins (Blues), Huw Bennett (Ospreys), Scott Andrews (Blues), Bradley Davies (Blues), Ian Evans (Ospreys), Dan Lydiate (Newport Gwent Dragons), Sam Warburton (Blues, capt), Toby Faletau (Dragons).
Matthew Rees (Scarlets, for Bennett 59), Ryan Bevington (Ospreys, for Andrews 59), Ryan Jones (Ospreys, for Lydiate 65), Justin Tipuric (Ospreys, for Evans 65), Tavis Knoyle (Scarlets, for L Williams 74), Dan Biggar (Ospreys, for Priestland 74), Alex Cuthbert (Blues, for North 69).
Adam Ashley-Cooper (NSW Waratahs); Lachie Turner (NSW Waratahs), Anthony Fainga'a (Queensland Reds), Berrick Barnes (NSW Waratahs), Digby Ioane (Queensland Reds); James O'Connor (Melbourne Rebels), Will Genia (Queensland Reds); James Slipper (Queensland Reds), Tatafu Polota Nau (NSW Waratahs), Salesi Ma'afu (Western Force), James Horwill (Queensland Reds, capt), Rob Simmons (Queensland Reds), Scott Higginbotham (Queensland Reds), David Pocock (Western Force), Ben McCalman (Western Force).
Stephen Moore (Brumbies, for Polota-Nau 52), Ben Alexander (Brumbies, for Ma'afu, 52), Nathan Sharpe (Western Force, for Simmons, 67), Radike Samo (Queensland Reds, for Pocock 41), Ben Tapuai (Queensland Reds, for Ashley-Cooper, 77), Nick Phipps (Melbourne Rebels).
Nick Phipps (Melbourne Rebels), Ben Lucas (Queensland Reds).
Jonathan Kaplan (South Africa).
Dave Pearson (England) and Jerome Garces (France).
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.