SACRAMENTO, Calif. —A California judge Monday ordered the release of a 17-year-old girl who had been placed in juvenile hall last month because prosecutors feared she would flee rather than testify at the upcoming trial of a suspected rapist.

Superior Court Judge Lawrence Brown previously ordered the girl held because she has a history of running away. On Monday, he said she would be released with a GPS ankle bracelet after prosecutors and the girl’s attorneys agreed to the release.

The judge told the teenager her case had charted rare legal territory because her detention was seen as victimizing the girl a second time.

“I am truly sorry for all that you’ve been through,” he told the teenager. “You’ve demonstrated great courage for a young woman.”

The Associated Press is not naming the girl because of her age and because she was the alleged victim of sexual assault.

Prosecutors took the unusual step of detaining the foster child in juvenile hall because they say her testimony is vital evidence against a man they believe is a serial rapist.

The teenager previously failed to make two court appearances when the suspect, Frank William Rackley Sr., 37, was previously charged, forcing prosecutors to dismiss charges against him.

They obtained a material witness warrant allowing her to be detained after they refiled the charges.

The teenager did not speak during Monday’s court hearing, except to smile and laugh with one of her attorneys.

“It’s a good day for my client,” said one her attorneys, Amina Merritt, who represents the girl’s interests as a foster child. “Obviously she was suffering incarceration, and I’m glad that she’s been released. She’s holding up amazingly well for a girl of her young age.”

The judge would not release the terms of the girl’s release or say where she would be living.

Victims’ rights advocates had said incarcerating the girl could discourage other victims from reporting sexual assaults, but prosecutors said the danger posed by Rackley outweighed the inconvenience to the teenager.

Rackley is accused of abducting and raping the girl last July, when she was 16. He also is charged with raping a 30-year-old suspected prostitute a month earlier, and prosecutors believe he is a serial rapist.

The girl’s other attorney, Lisa Franco, said her client has been treated like a criminal since she was incarcerated with juvenile delinquents March 23. Other girls want to fight her and she was inadvertently hit with pepper-spray used to break up a fight between two other juveniles, Franco said.

She said her client was inadvertently released from juvenile hall last week, but proved she is not a flight risk by going immediately to a Sacramento group home. She was then sent back to custody.

Merritt, the other attorney, said she believed prosecutors could have taken less extreme measures to ensure the girl would show up and testify.

“This may be the first time I’ve heard of a minor being subjected to that process,” she said. “I think it can be characterized … as bullying.”

The criminal case against Rackley is scheduled to go to trial April 23.