It is narrated from Sayyiduna Ibn Abbas (may Allah be pleased with him) that Sayyiduna Ali bin Abi Talib (may Allah be pleased with him) passed by an insane woman who had committed adultery, and Sayyiduna Umar (may Allah be pleased with him) had ordered that she be stoned. Sayyiduna Ali (may Allah be pleased with him) sent her back and asked Sayyiduna Umar (may Allah be pleased with him): "O Commander of the Believers! Will you stone her?" He replied: "Yes." Sayyiduna Ali (may Allah be pleased with him) said: "Do you not know that the Messenger of Allah (peace and blessings be upon him) said: 'The Pen (of accountability) has been lifted from three kinds of people: from the insane person whose mind is overcome until he comes to his senses, from the sleeper until he wakes up, and from the child until he reaches puberty'?"
Sayyiduna Umar (may Allah be pleased with him) said: "You have spoken the truth." So he set her free.
Imam Abu Dawud and Imam al-Nasa'i (may Allah have mercy on them) narrated it through Abu al-Tahir Yunus bin Ubayd.
Related hadith on this topic