SAN FRANCISCO — It started out as an operation to treat an increasingly common medical problem in America, childhood sleep apnea. It has become an anguished fight over the fate of a 13-year-old girl who, though pronounced legally dead by doctors, remains alive in the opinion of her religious parents.

The girl, Jahi McMath, was declared brain-dead after complications from surgery on Dec. 9 at Children’s Hospital Oakland, which wanted to remove her from a ventilator. But her heart continues to beat, and her family protested the removal in court, so she has remained connected to the machine.

On Friday, amid acrimonious battles in three courts, an Alameda County Superior Court judge mediated an agreement that could allow the child to be moved to another facility willing to take her, even though the hospital has declared her dead.

As arguments in the courts continue, the girl will remain connected to the ventilator at least until Tuesday, under the judge’s order. In the meantime, family members are scrambling to identify a facility that will accept the girl and doctors willing to carry out procedures that will keep her heart beating during the transfer.