If the petitioner has been legally committed for a criminal offense, or if upon hearing it appears by the testimony offered with the return that the petitioner is guilty of the offense, although the commitment is irregular, the judge before whom the petitioner is brought shall allow release on bail, if good bail is offered, or, if not, the judge shall immediately send that petitioner back to the detaining authority. In other cases the petitioner must be placed in the custody of the person legally entitled to custody, or, if no one is so entitled, the petitioner must be discharged.
Minn. Stat. § 589.16
(9754) RL s 4588; 1985 c 265 art 9 s 1