Mardy Fish waves to the U.S. Open crowd as he walks off the court after playing his last professional match. (AP Photo/Frank Franklin II)

Mardy Fish knew there wouldn’t be a Hollywood ending for him in New York. The 33-year-old announced earlier this year he would retire after the U.S. Open and entered his last tournament this week with the expectation that each match would be a bonus in a tennis career that nearly ended a few years ago.

On Wednesday afternoon, Fish fell in five sets to No. 18 seed Feliciano Lopez of Spain, 2-6, 6-3, 1-6, 7-5, 6-3, in the second round at Flushing Meadows. After the match, which lasted more than three hours, Fish walked off the court with tears in his eyes as he waved to the capacity crowd on Louis Armstrong Stadium for the last time as a professional player.

The moment marked the final chapter of Fish’s comeback after heart problems and an anxiety disorder that began in 2012 forced him to step away from the game.

“It doesn’t feel good for an athlete to sort of be pulled away from his or her job or what they do well,” Fish told Washington Post columnist Dan Steinberg last month. “I want people to know what I’ve gone through, to be a role model and a success story for people that maybe struggle with mental illness, and for people to remember my career in a positive light.”

Serving for the match at 5-4 in the fourth set against Lopez, a player Fish had led 5-3 in their head-to-head record entering Wednesday’s match, Fish began to show signs of cramping and was broken twice to lose the set, 7-5. Lopez took control in the fifth set and fired 12 winners to six unforced errors to advance to a third-round matchup against 10th seeded Milos Raonic of Canada, a 6-2, 6-4, 6-7 (5-7), 7-6 (7-1) winner over Spain’s Fernando Verdasco.

It was a scenario that didn’t seem possible during Fish’s hiatus from tennis, as he dealt with heart arrhythmias and an anxiety disorder, which he wrote about in candid detail in a Players’ Tribune article published Tuesday.

“I was having trouble sleeping; I couldn’t sleep alone,” said Fish, who reached a career high of world No. 7 in August 2011. “I had to have my wife there, with me, always. I had to have someone in the room, always. …And through it all, I just kept having these … thoughts. This anxiety. I became consumed by this exhausting, confusing dread.”

The last time Fish played in New York was in 2012, when he was set to compete against then world No. 1 Roger Federer in the round of 16. But the anxiety attacks, which he said happened every 10-15 minutes, spiraled to a point where he couldn’t control it and he withdrew from the match.

Fish would go on to post uneven results in 2013 and retired in the second round of the Winston-Salem Open in August that year. He would not compete again until this March. An attempt by his good friend and 2003 U.S. Open champion Andy Roddick to play doubles with Fish at the U.S. Open last year was derailed when Roddick was deemed ineligible for not making himself available for a drug test.

Roddick, who has known Fish since they were high school students in Florida, feared there would not be another chance for Fish to end his career on his own terms.

But Fish, who has been an outspoken mental health advocate, sounds at peace with how his career has unfolded and now wants to use his experiences to inspire others.

“This isn’t a sports movie, of course, and there won’t be a sports movie ending,” he wrote. “I won’t be riding off into the sunset, lifting a trophy. I’m not going to win the tournament. But that’s fine by me — because honestly, this isn’t a sports story.

“This is a life story. This is a story about how a mental health problem took my job away from me. And about how, three years later, I am doing that job again — and doing it well. …This is a story about how, with the right education, and conversation, and treatment, and mindset, the things that mental illness takes away from us — we can take them back. Tens of millions of Americans every year deal with issues related to mental health. And the journey of dealing with them, and learning to live with them, is a long one. It can be a forever one. Or, worse, it can be a life-threatening one. And I want to help with it.”