Police Commissioner Bill Bratton quietly reinstated two NYPD bosses who had been stripped of their guns and badges over corruption allegations — in a secret deal to get them to retire, The Post has learned.

The move — which ensures the high-ranking cops will get highly coveted, official references from the NYPD — came as Bratton is trying to oust all scandal-tainted brass by the end of the month, sources said Tuesday.

Deputy Chief John Sprague, whom Bratton tapped last year to head a newly created, internal-investigations unit, was placed on “modified duty” June 1 for refusing to answer questions in front of a federal grand jury.

Inspector Peter DeBlasio, operations coordinator for Patrol Borough Brooklyn South, was relieved of his duties on May 18 for the same reason.

But Sprague and DeBlasio were both technically returned to active duty as part of a deal to force their retirements, sources said.

DeBlasio refused to retire while on modified duty because it would have kept him from receiving an NYPD “Good Guy letter,” sources said.

The letter lets ex-cops easily obtain a full-carry pistol permit and is a boon to future employment.

Bratton even noted its importance on Monday while discussing the federal corruption charges filed against Deputy Chief Michael Harrington, Deputy Inspector James Grant and Sgt. David Villanueva.

A fourth cop, Officer David Ochetal, secretly pleaded guilty earlier and is cooperating with authorities, according to the Manhattan US Attorney’s office.

Bratton agreed to DeBlasio’s demand to be removed from modified duty and DeBlasio put in his retirement papers early last week, sources said.

Sprague also filed for retirement Friday after being returned to active duty, sources said.

Sprague was a particular embarrassment because Bratton hand-picked him to head the NYPD’s Force Investigation Division, sources said.

The NYPD’s top in-house lawyer, Larry Byrne, said: “There was no secret deal….They expressed a desire to retire and I indicated that if they retired, they would get their Good Guy letter.”

Byrne also said their “Good Guy letters” could be revoked at anytime, including if either was charged with a crime.