An illegal immigrant accused of raping a 13-year-old girl in Brooklyn was cut loose thanks to New York’s “sanctuary city” policy and the Brooklyn District Attorney — despite a request by immigration officials that he be detained.

Guatemalan national Miguel Federico Ajqui-Ajtzalam, 20, was arraigned Friday on a 20-count criminal complaint for allegedly raping the girl five times in 2018.

But Brooklyn DA Eric Gonzalez’s office didn’t ask the judge to set bail, and Criminal Court Judge Hilary Gingold agreed to let him go on supervised release.

Immigration and Customs Enforcement had issued a detainer asking the city to nevertheless keep Ajqui-Ajtzalam in custody because he is in the country illegally, but said the Big Apple did not comply due to its “sanctuary city” status.

ICE finally tracked Ajqui-Ajtzalam down Monday and took him into federal custody, but issued a furious rebuke of the city.

“It’s inconceivable that an individual who was arrested on local charges of rape and sexual abuse of a child would be released back into the community,” Thomas Decker, ICE removal operations director in New York, said in a statement Tuesday.

“It’s frightening that our detainer was ignored and he was released on to New York city streets to possibly re-offend.”

ICE typically allows local criminal cases to go ahead before any deportation, though immigration proceedings can happen concurrently, the agency said.

Ajqui-Ajtzalam entered the country illegally in 2016 as “an unaccompanied alien child” and was released pending deportation proceedings but went on the lam, according to ICE.

The Sunset Park man was still on the run when he had allegedly had sex with the underage girl between October and December 2018, according to a complaint filed in Brooklyn Criminal Court.

The girl was 13 at the time and he was reported on Feb. 22 of this year, police sources said.

The Brooklyn DA’s Office defended the decision by saying that Ajqui and the victim were allegedly in a “relationship” at the time of the offenses.

“The case was reported by the girl’s mother, alleging that her daughter had a sexual relationship with an older teenager, which constituted statutory rape,” a spokesman told The Post.

A spokesman for the Office of Court Administration defended the judge’s decision by saying: “The District Attorney’s office requested supervised release and the Judge consented, agreeing that supervised release was the most appropriate and least restrictive form of bail to ensure the defendant’s return to court.

In a statement, the NYPD said it “does not conduct civil immigration enforcement and we will only honor immigration detainers under the following circumstances: ICE presents a warrant issued by a federal judge establishing that there is probable cause to take the person into custody, and the person has been convicted of a ‘violent or serious crime’ within five years of the arrest or is a possible match on the terrorist watch list.”

Ajqui-Ajtzalam faces charges including second-degree rape, sexual misconduct and endangering the welfare of a child.

Additional reporting by Craig McCarthy