His bagel defense survived a schmear campaign.

A city correction officer won back his job after a yearlong struggle that all started when he chowed down on some poppy-seed bagels — and lit up a routine drug test with traces of morphine and codeine.

The amounts were enough to get him fired, until an obscure city commission took the extraordinary step of overruling the Correction Department’s commissioner.

Officer Eleazar Paz was reinstated to his $82,000 post on Christmas Eve, bringing to full circle a back-and-forth legal battle that spanned nearly all of 2018.

“I wanted to cry when I heard they would take me back,” the 49-year-old Rikers officer said Wednesday.

“It was unbelievable what happened to me, because I didn’t do anything wrong.”

Paz thought his career was over until his Hail Mary appeal to the Civil Service Commission, which typically resolves much more mundane disputes.

In a Nov. 29 ruling, the panel reversed Correction Commissioner Cynthia Brann’s firing of Paz, which itself flew in the face of a recommendation by an administrative trial judge that Paz keep his job.

The seedy saga began in January, when Paz, for the first time in his 10-year career flunked his drug exam.

Paz insisted that poppy-seed bagels he had eaten on both the day before and day of the test were to blame, a claim that was backed up at his administrative hearing by toxicology expert Dr. William Sawyer.

Opium is derived from the poppy plant, and while the seeds don’t contain the drug, they can pick up opiate residue if they aren’t thoroughly washed.

The results of Paz’s test — 522 nanograms per milliliter of morphine and 358 of codeine — were not only inconsistent with drug abuse, but well under the federal threshold of 2,000 NPM, Sawyer said.

The feds raised their standard in 1997 precisely because unwashed poppy seeds in food were producing false positives, but the city’s jail system still uses the much more stringent 300 NPM, Sawyer said.

Judge John Spooner agreed and recommended Paz be rehired.

Brann still had more than a seed of doubt and booted him in July.

Paz appealed to the commission, which reversed the firing late last month, ordering him back to work within 30 days.

The Correction Department said he won’t be getting get back pay.

But Paz’s lawyer, Megan Rha of Rha & Kim LLP, said she’s ready to go back to court for the dough.

Paz said, “This should be a wake-up call. Every other agency has changed its standards; why hasn’t DOC?

“If this happened to me, it could happen to someone else.”

Correction officials refused to say if they would consider updating its standards.

In the meantime, Paz has sworn off the problematic poppies — including poppy-seed muffins, one of his favorites — for good, with his wife, Cristina, reading the labels of every food she buys.