Section 331 in The Code of Criminal Procedure
Title: Resumption of inquiry or trial
(1) Whenever an inquiry or a trial is postponed under section 328 or section 329, the Magistrate or Court, as the case may be, may at any time after the person concerned has ceased to be of unsound mind, resume the inquiry or trial and require the accused to appear or be brought before such Magistrate or Court.
(2) When the accused has been released under section 330, and the sureties for his appearance produce him to the officer whom the Magistrate or Court appoints in this behalf, the certificate of such officer that the accused is capable of making his defence shall be receivable in evidence
Title: Procedure on accused appearing before Magistrate or Court
(1) If, when the accused appears or is again brought before the Magistrate or Court, as the case may be, the Magistrate or Court considers him capable of making his defence, the inquiry or trial shall proceed.
(2) If the Magistrate or Court considers the accused to be still incapable of making his defence, the Magistrate or Court shall act according to the provisions of section 328 or section 329, as the case may be, and if the accused is found to be of unsound mind and consequently incapable making his defence, shall deal with such accused in accordance with the provisions of section 330.
Title: When accused appears to have been of sound mind
When the accused appears to be of sound mind at the time of inquiry or trial, and the Magistrate is satisfied from the evidence given before him that there is reason to believe that the accused committed an act, which, if he had been of sound mind, would have been an offence, and that he was, at the time when the act was committed, by reason of unsoundness of mind, incapable of knowing the nature of the act or that it was wrong or contrary to law, the Magistrate shall proceed with the case, and, if the accused ought to be tried by the Court of Session, commit him for trial before the Court of Session
Title: Judgment of acquittal on ground of unsoundness of mind
Whenever any person is acquitted upon the ground that, at the time at which he is alleged to have committed an offence, he was, by reason of unsoundness of mind, incapable of knowing the nature of the act alleged as constituting the offence, or that it was wrong or contrary to law, the finding shall state specifically whether he committed the act or not
Title: Person acquitted on such ground to be detained in safe custody
(1) Whenever the finding states that the accused person committed the act alleged, the Magistrate or Court before whom or which the trial has been held, shall, if such act would, but for the incapacity found, have constituted an offence,---
(a) order such person to be detained in safe custody in such place and manner as the Magistrate or Court thinks fit; or
(b) order such person to be delivered to any relative or friend of such person.
(2) No order for the detention of the accused in a lunatic asylum shall be made under clause (a) of sub- section (1) otherwise than in accordance with such rules as the State Government may have made under the Indian Lunacy Act, 1912 (4 of 1912).
(3) No order for the delivery of the accused to a relative or friend shall be made under clause (b) of sub-section (1) except upon the application of such relative or friend and on his giving security to the satisfaction of the Magistrate or Court that the person delivered shall---
(a) be properly taken care of and prevented from doing injury to himself or to any other person;
(b) be produced for the inspection of such officer, and at such times and places, as the State Government may direct.
(4) The Magistrate or Court shall report to the State Government the action taken under sub-section (1).