It is the duty of the court or judge forthwith to remand the party, if it appears that he is detained in custody, either -
(1) By virtue of process issued by any court or judge of the United States, in a case where such court or judge has exclusive jurisdiction.
(2) By virtue of the final judgment or decree of any competent court of civil or criminal jurisdiction, or of any execution issued upon such judgment or decree.
(3) For any contempt specially and plainly charged in the commitment by some court, officer or body having authority to commit for the contempt so charged.
(4) That the time during which such party may be legally detained has not expired. (1868-9, c. 116, s. 21; Code, s. 1646; Rev., s. 1848; C.S., s. 2236.)