For 15 months, Daniel Davon-Bonilla sat in the Rikers Island jail complex in New York City, accused of raping a transgender woman in a migrant shelter.

Then, on June 24, Mr. Davon-Bonilla stood before a judge in a Brooklyn court. The victim in the case had refused to testify, and now prosecutors were offering him a deal: He could plead guilty to a felony assault charge and be released that day.

The judge, Donald Leo, warned Mr. Davon-Bonilla, a 24-year-old from Nicaragua, that he could be deported.

“Do you still wish to proceed with your plea of guilty?” asked Justice Leo, according to a transcript of the hearing.

“Of course,” Mr. Davon-Bonilla replied.

In fact, the federal Immigration and Customs Enforcement agency, known as ICE, had informed the city that it intended to deport Mr. Davon-Bonilla when he was first charged with rape, the agency said. But neither the city nor the Brooklyn district attorney notified federal officials when he was released that day in June.

Mr. Davon-Bonilla did not show up for his sentencing on Aug. 9. Two days later, the police say, he raped a homeless woman under the Coney Island boardwalk.