The 23-times Olympic gold medal winner said he wanted to talk openly about his depression to encourage others to seek the help he got.
Phelps, who is the most decorated Olympian of all-time with 28 medals, said he remained in his room for four days without food or sleep after the 2012 London Games when he won four golds and two silvers.
He said: "I didn't want to be in the sport anymore. I didn't want to be alive. You do contemplate suicide."
The American sports star said he had battled depression and anxiety for the past 17 years.
"We're supposed to be this big, macho, physically strong human beings, but this is not a weakness," he said.
"We are seeking and reaching for help."
Phelps won his first gold medal in 2004 at the Athens Olympic Games and it was that year the 15-year-old from Baltimore experienced his first "depression spell".
He said it had led him to abuse drugs and alcohol and in 2008, after winning a record eight gold medals at the Beijing Games, he was photographed smoking from a bong.