The veteran NYPD detective accused of fondling himself in front of young girls on Long Island was coerced by his own brothers in blue into giving a false confession, his lawyer claimed Monday.

“They played on his trusts and put him through a process that he didn’t think was aimed at him — and wound up making him a suspect,” Peter Brill, defense attorney for Robert Francis, told The Post on Monday after his client’s arraignment.

“These were people he believed were on his side,” he said. “Ultimately, we have a confession that he completely denies making, and he maintains his innocence on these charges.”

Francis, who is currently suspended, pleaded not guilty in Nassau County District Court to public lewdness, endangering the welfare of a child, and criminal trespass.

He allegedly waived his Miranda rights and admitted to exposing himself and masturbating in front of four girls, between the ages of 14 and 17, following his arrest in Rockville Centre on March 26.

But Brill said his client never did such a thing — and on top of that, he was never identified in a police lineup. The lawyer claimed cops also ignored the fact that at least three of the victims had identified their flasher as being a white male.

“Robert Francis is far from white,” Brill said. “That would be an impossibility for someone to misidentify him as being white. Why not do any sort of identification procedure? Why no lineup?”

Brill told The Post he believes Francis, who is a military veteran, was ultimately used as a scapegoat by village police after they were unable to catch the real pervert.

“What we have here is a small police department, for a mostly upper class community, with a series of disturbing reports of someone committing these acts int heir backyard, and they seized on Robert Francis and they made the facts fit their suspicion, as opposed to what they’re supposed to do, which is let their suspicion fit the facts.”

Francis, who is from Queens, was released on his own recognizance Monday with consent from the district attorney’s office, Brill said.

The lawyer described the 15-year NYPD veteran as an “upstanding member of the community” who simply got taken advantage of by his former colleagues.

“To suggest that he was running around people’s backyards, touching himself because he was under stress, it makes no sense,” he said of his confession, in which Francis allegedly stated he was going through a rough time in his life.

“He was in that neighborhood for a completely innocent reason,” Brill added. “A social visit.”