The bailor at any time before final judgment against him upon a forfeited recognizance, may surrender his principal in open court or to the sheriff; and upon the payment of all costs occasioned by the forfeiture, and all costs that may accrue at the term to which the prisoner was recognized to appear, may thereupon be discharged from any further liability upon the recognizance.