It was a race to save his life.

A marathon trainee dodged death on the running course over the weekend, thanks to a pair of fast-acting NYPD police officers who were hailed as heroes on Wednesday.

The 44-year-old man, who didn’t want to be identified, was nearing the end of an 18-mile loop in Central Park on Sunday when he suddenly collapsed in cardiac arrest near West 74th Street and West Drive.

Officers Benjamin Beiro, 30, and Nicholas Noto, 25, sprang into action after getting the 911 call around 9:30 a.m., with Beiro beginning chest compressions on the runner and Noto dashing to get a defibrillator from their patrol car.

“In a condition like this, every second counts,” Noto, who has four years on the force, said at a press conference. “So the quicker you get there … the better the result.”

After strapping the pads onto the victim’s chest, Beiro continued the compressions while Noto delivered two life-saving counter-shocks that restored the man’s pulse.

“Shortly after that, the male began to gasp. We got a faint pulse and he began to wake up,” Beiro recalled.

The man regained consciousness just before EMS arrived to transport him to Mount Sinai West hospital.

That’s when the man’s wife — who began searching for him when he didn’t cross the finish line as expected — received a call from an emergency room doctor.

“In that moment, the whole world stopped and I feared the worst,” she said in a letter to the officers. “The ER doctor on the other end of the phone then said, ‘Don’t worry, he’s going to be OK.'”

Both officers visited the victim, who was transported to Mount Sinai St. Luke’s.

“He was doing good,” said Beiro, who’s been with the NYPD for 6½ years. “He definitely expressed a lot of gratitude.”

The officer added, “It was definitely one of those moments where you feel great to be doing the job you do, just to be able to help people.”

The runner was close to completing the TCS New York City Marathon Training Series 18M — three six-mile loops of Central Park — when his heart gave out.

In a statement, he thanked the cops for their “quick arrival, assessment and actions” that spared his life one week from his first wedding anniversary.

“You are a testament to your excellent training and your dedication to serving your community,” the man wrote. “I am personally in awe.”

He added that doctors have figured out the “developmental defect” that caused his cardiac arrest and that he’s discussing further treatment.

His grateful wife also gushed over the officers’ response.

“In the past two days I have thought back to that moment a lot. Because if not for the quick, efficient and effective response from both of you, the worst would have actually happened,” she wrote. “I wish you the best in your careers, and hope the same level of kindness comes back to you when you need it most.”