The sheriff shall take charge of, safely keep, and keep a correct account of, all money and valuables found on each prisoner when delivered at the county jail. Except when otherwise ordered by a court of competent jurisdiction, the sheriff shall pay such money or sums therefrom and deliver such valuables or portions thereof as the prisoner directs and shall pay and deliver all the remainder of his money and valuables to the prisoner or to his order upon his release from the jail or to his legal representative in case of his death or legal incapacity to make decisions.