HOUSTON — U.S. authorities on Friday released a 10-year-old immigrant girl with cerebral palsy who had been detained by border agents after surgery because she is in the U.S. without legal permission.

The ACLU and U.S. Rep. Joaquin Castro said in statements that Rosa Maria Hernandez was returned Friday afternoon to her family. Her parents brought her into the U.S. from Mexico in 2007, when she was a toddler, and they live in the Texas border city of Laredo.

A cousin who is an American citizen took Rosa Maria from Laredo to a children’s hospital in Corpus Christi on Oct. 24, where she was scheduled to have emergency gallbladder surgery. To get to Corpus Christi, about 150 miles away, she had to pass through an interior checkpoint in South Texas operated by the Border Patrol.

Border Patrol agents followed Rosa Maria and the cousin to the hospital, then took the girl into custody after the surgery and transported her to a facility in San Antonio for unaccompanied immigrant minors, under the custody of the U.S. Department of Health and Human Services.

The Border Patrol has said it had no choice but to detain Rosa Maria, arguing that she was considered an unaccompanied minor under federal law, the same as a child who crosses into the United States alone without legal permission.

The ACLU sued the government on Rosa Maria’s behalf Tuesday, argued that the U.S. government violated federal law on unaccompanied minors and endangered Rosa Maria’s health by not sending her home.

“She never should have been in this situation in the first place,” ACLU lawyer Michael Tan said Friday. “There is no reason Border Patrol had to target a child.”

While Rosa Maria has been reunited with her family, she still faces the threat of deportation. Tan said Friday that Border Patrol agents had issued Rosa Maria a notice to appear in immigration court, but that the case had yet to move forward.

U.S. Customs and Border Protection, which oversees the Border Patrol, declined to comment.