The Trump administration must allow hearings for undocumented teenagers who were living with relatives or friends in the United States, with government approval, when they were arrested in gang sweeps last year and sent to lockups in distant states, a federal appeals court in San Francisco ruled Monday.

The Ninth U.S. Circuit Court of Appeals upheld a November 2017 injunction by U.S. District Judge Vince Chhabria of San Francisco ordering immigration courts to hold immediate hearings for the teens and release them to a family member or another sponsor unless the government could show they were dangerous or likely to flee if released.

The case involves youths who entered the U.S. without legal documentation, and without their parents, but were released to live with a sponsor after immigration officials concluded they were not dangerous. They were picked up last year in raids aimed at unauthorized immigrants suspected of gang membership, and then transferred to secure detention centers.

The lead plaintiff, who had fled an abusive father in Honduras and entered the U.S. in 2015, was living with his mother in New York at the time of his arrest, and was flown across the country to the Yolo County Juvenile Detention Facility in Woodland, where he was placed under high security.

Chhabria’s November injunction led to immediate immigration court hearings for 30 to 35 youngsters, and a few more since then, most of whom won release to their original sponsors, said Stephen Kang, an American Civil Liberties Union lawyer.

While the youths are out of custody, Kang said, more remains at stake in the case. He said the Trump administration’s Office of Refugee Resettlement “has been using flawed gang allegations to deny important immigration benefits” to the youths. Those benefits include eligibility for asylum, based on a likelihood of persecution in their homeland, and another program allowing youths who have been abused, abandoned or neglected by a parent to apply for legal U.S. residence, Kang said.

ACLU lawyers said immigration officials have classified youths as gang members based on the color of the shirt they wore, the pizza parlor they frequented or a number one teenager wrote down that was considered a code for El Salvador.

In appealing Chhabria’s injunction, the Trump administration agreed that the youths were entitled to challenge their transfer and detention, but argued that they were adequately protected by the government’s own review procedures. The appeals court disagreed.

The internal process “is entirely unilateral,” Judge Andrew Hurwitz said in the 3-0 ruling. “The juvenile is not provided with notice of the reason for incarceration or an opportunity to answer any charges,” rights that are provided at an immigration court hearing.

Bob Egelko is a San Francisco Chronicle staff writer. Email: begelko@sfchronicle.com Twitter:@BobEgelko