New York City saw a nearly fivefold increase in heroin overdose deaths per 100,000 residents between 2010 and 2015. Similarly, opioids were linked to more than 42,000 deaths nationwide in 2016, five times the 1999 rate, according to the Centers for Disease Control and Prevention.

John Kane, 47, completed treatment about five months ago at Odyssey House, which offers inpatient and outpatient treatment for adolescents and adults at sites throughout the city. Mr. Kane, who is originally from Long Island, said he became addicted to prescription opioids nearly 20 years ago, and later got hooked on heroin.

The team, he said, has given him a drive that transcends the time he spends in sneakers.

“It’s also transferable to everything else I do in life,” Mr. Kane said. “The hard work, the perseverance, the dedication it takes to run a marathon can cross over into your everyday life — as far as setting a goal, working toward that goal and achieving that goal.”

He now has steady construction work and is looking to finish his second marathon on Sunday in under four hours.

Andrew Caceres said he was 19 and living in the Hudson Valley when he got hooked on heroin. He crashed a car while high, putting him on a path of legal trouble that landed him in and out of jail and eventually at Odyssey House, where he is living.

Mr. Caceres, who has a 3-year-old son and a 4-month-old daughter, has just begun training as a heating and air-conditioning technician. He credits the running team with helping him get into the best physical shape of his life.

“I’m at that point,” Mr. Caceres, 25, said. “I want to be with my daughter. I want to be with my son. I want my freedom.”