Section 22. Except as otherwise provided, county treasurers, clerks of the courts, clerks of district courts, sheriffs and superintendents of jails and houses of correction, probation officers, registers of probate and insolvency and registers of deeds, having more money in their hands than is required for immediate use, shall deposit it, in their official names, in national banks or trust companies in the commonwealth or banking companies doing business in the commonwealth and qualified to receive demand deposits under the provisions of section six A of chapter one hundred and seventy-two A, at the best practicable interest rates. County treasurers may also deposit in time deposits in such national banks, trust companies or banking companies and invest in United States treasury bills. Interest thereon shall be paid to the county, except that interest accruing to deposits by registers of probate and clerks of courts shall be paid to the commonwealth; provided, that interest accruing on the deposit as aforesaid of any money paid to any official mentioned in this section which is so paid under order of a court or which is otherwise subject to the direction of a court shall, if the court so directs, be paid to the parties entitled to the principal fund of such deposit.