It is narrated from Sayyidah Aisha (may Allah be pleased with her) that the Noble Prophet (peace and blessings be upon him) said: "The pen has been lifted from three persons (i.e., they are not held accountable for their good or bad deeds): from the one who is sleeping until he wakes up, from the child until he reaches puberty, and from the insane until he regains sanity."
In another narration, Hammad used the word "ma‘tuh" instead of "majnoon"; both have the same meaning, i.e., mad or insane, until he regains sanity.
Hadith Referenceسنن دارمي / من كتاب الاحدود / 2333
By "hudud" are meant those punishments which are fixed and prescribed for known sins.
From this hadith, it is understood that whatever good or bad deed is committed by these three persons, it is not recorded, and there is no reckoning for it with Allah Ta'ala; therefore, neither will the prescribed punishment (hadd) be carried out upon them, nor will divorce (talaq) or sale (bay‘) be considered valid. Thus, if a person, while asleep or in a state of insanity, pronounces divorce or emancipation (‘itaq), or commits any other good or bad deed, he will not be held accountable for it.
Imam Darimi rahimahullah, after narrating this hadith in the Book of Hudud, has indicated that the prescribed punishments (hudud) will also not be carried out upon these three persons.
And in contrast to them, if a person who is awake, of age (baligh), and of sound mind (sahih al-‘aql) commits a sin, then he will be held accountable and the prescribed punishment (hadd) will be carried out. If he pronounces divorce or orders emancipation, it will be effective; the divorce will take place and the slave will be freed.
By "hudud" are meant those sins for which punishments have been prescribed in this world, such as fornication (zina), theft (sariqa), highway robbery (hirabah), apostasy (irtidad), murder (qatl), false accusation (qadhf), drinking alcohol (shurb al-khamr), etc. The explanation of all these will come ahead.