Whenever it shall be lawful and necessary to confine any prisoner in custody previous to conviction upon a criminal accusation, or in custody for contempt or alleged contempt of court, or upon an attachment by order of a court or judge, or otherwise in lawful custody, or upon conviction for any offense, the officer or person having him in such custody may convey him to and confine him in the jail of any county in this state, or other secure and convenient place of confinement in this state, to be procured by such officer or person having such prisoner in custody.