"I say goodbye to football but not to you," the Swede told the San Siro crowd after Sunday's last game of the season.
Ibrahimovic had already announced he would leave the Italian club.
He scored 511 goals for clubs including Paris St-Germain, Manchester United and both AC and Inter Milan, winning league titles in four countries.
Ibrahimovic began his second spell with AC Milan in early 2020, having won the Scudetto with them in 2011, and helped them win the title again last season.
But he played only four times and started one match for the Serie A side this term - and scored one goal - following a series of injuries, and his contract was set to expire this month.
"There are too many emotions for me right now. Forza Milan and goodbye," an emotional Ibrahimovic said, holding back tears.
"The first time I came here you gave me happiness, the second time you gave me love.
"You welcomed me with open arms, you made me feel at home, I will be a Milanista for the rest of my life."
Ibrahimovic won 34 trophies - including 14 league titles - in a career that began in the last century, and was nominated for the Ballon d'Or 11 times.
The one major club trophy that eluded him was the Champions League, with his only European title coming with Manchester United in the shape of the 2017 Europa League.
The striker also retires as Sweden's all-time top scorer with 62 international goals in 122 matches.
He quit the national side after Euro 2016 but returned in 2021 for their unsuccessful World Cup qualifying campaign.
"I used to be scared when journalists asked by about my future, but now I can accept it, I'm ready," he told reporters after his announcement.
"I've been doing this all my life. Football made me a man. It allowed me to know people I otherwise would never have known. I've travelled the world thanks to football. It's all thanks to football."
A career laced with gold
Ibrahimovic started his professional career with hometown side Malmo FF in 1999 before moving to Ajax in 2001 for a three-year spell in which he won three league titles.
A move to Italy followed in 2004 with Juventus, where he won two league titles, both of which were later stripped from the club as a result of the Calciopoli scandal.
However, the Swede won three more Serie A titles with his next club, Inter Milan, before a move to Barcelona in 2009.
Ibrahimovic spent only one season at the Catalan club, winning a La Liga title before being loaned to AC Milan, a move which was made permanent in 2011.
A year later, however, and Ibrahimovic was on the move again, signing for Paris St-Germain, where he scored 113 goals in 122 league appearances and won four Ligue 1 trophies.
In July 2016, he signed for Manchester United for a two-year spell with the Premier League club that saw him win the League Cup and the Europa League.
Ibrahimovic signed for MLS side LA Galaxy in 2018, spending two seasons in California before making his return to AC Milan.