Section 334 in The Code of Criminal Procedure
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).
Title: Power of State Government to empower officerincharge to discharge
The State Government may empower the officer in charge of the jail in which a person is confined under the provisions of section 330 or section 335 to discharge all or any of the functions of the Inspector-General of Prisons under section 337 or section 338
Title: Procedure where lunatic prisoner is reported capable of making his defence
If such person is detained under the provisions of sub-section (2) of section 330, and in the case of a person detained in a jail, the Inspector-General of Prisons, or, in the case of a person detained a lunatic asylum, the visitors of such asylum, or any two of them shall certify that, in his or their opinion, such person is capable of making his defence, he shall be taken before the Magistrate or Court, as the case may be, at such time as the Magistrate or Court appoints, and the Magistrate or Court shall deal with such person under the provisions of section 332; and the certificate of such Inspector-General or visitors as aforesaid shall be receivable as evidence
Title: Procedure where lunatic detained is declared fit to be released
(1) If such person is detained under the provisions of sub-section (2) of section 330, or section 335, and such Inspector-General or visitors shall certify that, in his or their judgment, he may be released without danger of his doing injury to himself or to any other person, the State Government may thereupon order him to be released, or to be detained in custody, or to be transferred to a public lunatic asylum if he has not been already sent to such an asylum; and, in case it orders him to be transferred to an asylum, may appoint a Commission, consisting of a Judicial and two medical officers.
(2) Such Commission shall make a formal inquiry into the state of mind of such person, take such evidence as is necessary, and shall report to the State Government, which may order his release or detention as it thinks fit.