Federal agents nabbed an undocumented immigrant as he was leaving a Brooklyn court Friday, sparking a protest by dozens of public defenders.

The three plainclothes Immigration and Customs Enforcement agents had waited outside the courtroom at Brooklyn Criminal Court while Panamanian man Diogenes Pinzon appeared inside, attorneys said.

When the 38-year-old emerged around 11 a.m., they slapped him in cuffs and hauled him off — leading around 50 lawyers from Legal Aid and Brooklyn Defender Services to walk out in protest, toting signs reading, “ICE out of the courts,” and chanting, “No justice, no racist police.”

“It’s atrocious that our city pretends to be a sanctuary city but people can be picked up from their court appearance,” said Legal Aid lawyer Rage Kidvai.

Pinzon’s attorney said he was planning on heading to his job as a barber after the hearing and has a 4-year-old son at home.

“It’s disturbing,” said Ronald Sylvestre, who says he emerged from the courtroom after Pinzon only to see his client already in handcuffs. “By coming to court to dispose of his case, he shouldn’t be arrested.”

This isn’t the first time the Brooklyn attorneys have taken to the streets to protest an ICE arrest at the courthouse — there was a similar scene when a Mexican immigrant was dragged off in November.

The agency says it doesn’t consider courthouses a “sensitive location” for arrest.