A judge on Wednesday ordered a 14-year-old girl be returned to Massachusetts after she was taken by federal agents to a facility for minors in New York.

District Judge Leo T. Sorokin ordered that the girl, who is listed under the pseudonym B.E.S in court documents, leave the facility at 9 a.m. on Thursday and be reunited with her aunt, who is a U.S. Citizen. The aunt is required to file an affidavit detailing her name, status as a citizen and her intent to be the teenager’s temporary or permanent guardian. The teenager’s lawyer, Andrew Lattarulo, and Rayford Farquhar, a lawyer for the U.S. District Attorney’s Office, agreed on the judge’s ruling.

The teenager was born in Brazil and entered the United States on a visitor’s visa in 2019. According to Farquhar, the teenager — who attends the Brighton Public Schools — was in a vehicle in Marlborough which was pulled over by authorities on March 10.

He said there were other individuals in the vehicle who did not have legal status to be in the country and that the girl was without her caretaker. At the time, the girl was living with her two brothers, both of whom are over 21, according to Lattarulo.

She had previously lived with her father, who is now “out of the picture,” Lattarulo said.

Farquhar says it is his understanding that the authorities who pulled the vehicle over were local officers, who then contacted federal agents.

Lattarulo, however, said he wasn’t sure if that was the case.

“If the child was in school, I would assume it’d be like a truancy officer,” Lattarulo said.

The teenager was then taken into custody by agents of the Office of Refugee Resettlement, a government agency under the Department of Health and Human Services.

Court documents say the girl was taken to the Marlborough police department, where she waited several hours. Officers tried to contact the girl’s father to have her released, court documents read. After she wasn’t released, she was taken to the John F. Kennedy Federal Building in Boston. The teenager was then taken to the contracted juvenile facility in New York, arriving after 7 a.m. on Wednesday, according to Farquhar.

Judge Sorokin questioned why she was taken to New York to which Farquhar replied that the closest available bed for the child was located in the state.

Sorokin expressed confusion and surprise when he learned federal agents didn’t release the child to state authorities.

He said that when agents arrest parents, they contact an agency such as the Department of Children and Families.

“They don’t take the person into custody,” he said. “They just turn them over to the state authorities.”

The teenager is expected to leave the New York facility by 9 a.m. on Thursday.