Sonnen, 37, announced the news on Wednesday's edition of UFC Tonight, for which he serves as a UFC analyst. It marks the end of a 17-year professional career, which began in May 1997.
On Tuesday, the UFC was forced to pull Sonnen (28-14-1) from a light heavyweight bout against Vitor Belfort, scheduled for July 5 in Las Vegas, due to the failed drug test.
The results of the test confirmed the presence of anti-estrogen medications anastrozole and clomiphene. Both are listed on the World Anti-Doping Agency banned substance list.
Sonnen, who had been previously approved for the use of testosterone-replacement therapy (TRT), said he took the substances to prevent side effects from getting off TRT, which was banned earlier this year. He said he also required them for fertility reasons.
Sonnen, who was facing a likely suspension, said his legitimate medical need for estrogen blockers would not allow him to stop taking them, leaving retirement as his only option.
"I want to stay within the rules," Sonnen said. "I have to put my health first and my family first. If I know what I know now, that (these substances) are going to work, get my wife pregnant, let me have a family, I will do the exact same thing 20 times.
"If it puts me out of compliance 20 more times, then let's stop. I will remove myself. There is going to be a day where I don't have the title of fighter. That was always going to happen. I never want there to be a day where I don't have the title of parent, father and husband. My health has got to come first."
Sonnen thanked his fans, team members, Fox Sports, UFC executives and Nevada State Athletic Commission board member Bill Brady. He also thanked former UFC middleweight champion Anderson Silva, whom he challenged for the title in 2010 and 2012.
"I want to thank the single most important opponent I ever had," Sonnen said. "I couldn't have gotten to where I am without this dance partner and that is, of course, Anderson Silva. I thank you for the opportunity and the memories."
Sonnen's career took off in 2009, when out of nowhere he began mocking Silva in media interviews and public events. He bested Silva through four rounds of a title fight at UFC 117 in August 2010 before submitting to a triangle choke in the final round.
After the fight, Sonnen tested positive for an elevated testosterone-to-epitestosterone ratio, which eventually earned him a six-month suspension. It also led to him revealing a diagnosis of hypogonadism and being approved for TRT.
A rematch against Silva, which took place at UFC 148 in July 2012, remains the highest-grossing UFC event ever held in Las Vegas. The event drew an attendance of 15,016 and a live gate of $7 million. Sonnen lost via TKO in the second round.
Nine months later, Sonnen fought Jon Jones for the UFC light heavyweight title at UFC 159 in Newark. Jones dominated en route to a first-round TKO.
Competing mostly at middleweight, Sonnen's resume includes wins over Yushin Okami, Nate Marquardt, Brian Stann, Michael Bisping and Mauricio Rua. A former NCAA All-American collegiate wrestler, he was known for a smothering high pace and his uncanny ability to promote a fight.