Alongside a picture of him waving goodbye, the 35-year-old Spain international wrote on Twitter: "Lived it. Loved it. Farewell beautiful game."
He won club honours with Liverpool, Real Madrid and Bayern and the World Cup and two European Championships.
Alonso told Bayern's TV channel: "It wasn't an easy decision to make, but I believe it's the right time.
He added: "I always thought it would be better to quit sooner rather than later. I still feel good, but I believe this is the right moment."
Alonso joined Liverpool from Real Sociedad in 2004 and was part of a famous Champions League victory in Istanbul in his first season as the Reds came back from 3-0 down at half-time to beat AC Milan on penalties. He also claimed an FA Cup, a Uefa Super Cup and a Community Shield during his time at Anfield.
He moved to Real Madrid in 2009 and won a second Champions League as well as the Spanish title before a switch to Germany in 2014, where he has helped Bayern win two Bundesliga titles and reach the quarter-finals of this year's Champions League - courtesy of a 10-2 aggregate win over Arsenal on Tuesday.
Alonso played 114 times for Spain and scored 16 goals as the national side won the 2008 and 2012 European Championships, either side of their maiden World Cup triumph in South Africa in 2010. His final appearance for his country came at the 2014 World Cup in Brazil.
Media playback is not supported on this device

Alan Knight, 48, and wife Helen, 34, of Sketty, Swansea, had admitted perverting the course of justice.
Helen Knight was jailed for 10 months at Swansea Crown Court on Friday.
Her husband, who was jailed in November 2014 on fraud and forgery charges, will serve another 14 months on top of his current prison term.
Judge Huw Davies told Knight - appearing via videolink from prison - and his wife they had both committed an "extreme" example of intending to pervert the course of justice.
He said: "It resulted in significant delay in bringing the offender to justice."
He said this added to the distress of those facing giving evidence as Knight's trial was delayed time and time again over a period of two years.
The judge called Knight's deception "calculated" and told his wife: "All in all your behaviour was unscrupulous and selfish."
The judge told the court the couple came up with their elaborate plan to feign a "catastrophic" illness to try to stop the husband's trial for theft, fraud and forgery.
He had been accused of taking Â£40,000 of the the life savings of his elderly neighbour, who had dementia.
Every time someone in authority visited Knight, he pretended he was paraplegic and often in a coma.
He admitted himself to hospital for a total of 10 weeks to avoid the trial and convinced some GPs about his mystery illness.
Numerous tests were carried out but they all concluded he had nothing physically wrong with him.
Meanwhile, his wife wrote to the prime minister, the deputy prime minister and her local MP and AM, telling them her husband was being forced to go to court even though he was a paraplegic and in a permanent vegetative state.
Her local MP even took on her case, writing to Mr Knight's solicitors.
Eventually their lies were exposed after police found CCTV and picture evidence from their computer.
The photographs showed them on holiday, on day trips to places like Legoland and at a friend's wedding.
Bayern Munich midfielder Xabi Alonso has confirmed he will retire when his contract expires in the summer.
[NEXT_CONCEPT]
A man who faked being in a coma for two years to avoid a fraud trial has been jailed along with his wife for trying to con the courts.