The 32-year-old flanker, who bows out with 76 caps, admitted he could not think of a worse way to finish his Test career.
Head coach Gareth Jenkins was sacked after Wales' 38-34 defeat to Fiji sent them crashing out of the tournament in the group stages.
"I just think it's the right time to step aside so that whoever comes in as coach can start afresh," Williams told the Western Mail.
"International rugby works in four-year cycles these days with the World Cup so now's the time to go.
"I've had 11 years and I have achieved way more than I thought I would.
"It's been an honour and a privilege to play for Wales. It's meant a hell of a lot to me but everything has to come to an end and that is me done now.
"It's not the way I would have wanted it to end. I couldn't think of a worse way for it to finish to be honest, but that is how the cards fall some times.
"I'd told the boys beforehand that I was finishing, so they knew that was the last one for me."
Williams made his Test debut against France in 1996 and captained his country on five occasions.
He thrived in the Welsh style of open running rugby and was a key figure in Wales' 2005 Grand Slam campaign, when a string of outstanding performances earned him the player of the tournament award.
Williams was selected on the 2001 and 2005 Lions tours and played in three World Cups for Wales.
He will continue playing for the Cardiff Blues for the next two seasons but is also keen to develop a career outside of the game in commercial banking.
WRU chief executive Roger Lewis said: "I'm sure everyone would want to thank Martyn for being a total servant of the game.
"No-one could ask more of a player."
Williams could be followed into retirement by captain Gareth Thomas, who won his 100th cap on Saturday but admitted after the game that Wales should now build for the future.
Williams' departure leaves Wales with a gaping hole to fill at openside flanker, a position he has made his own in recent years.
He was the only specialist number seven in the World Cup squad. The versatile Colin Charvis provided back up but he too is closing in on retirement.
Charvis has won 94 caps but said he is unlikely to join Thomas as the second Welsh member of the 100 club.