There are crazy people who do things because they are crazy ... acting out of their craziness.
For example ... crazy guy sincerely believes that his parents and sibling have been replaced by identical duplicates but are now demons.
Kills parents, wounds brother, who manages to escape to call police. Crazy guy is apprended.
This is a classic Not Guilty By Reason of Insanity.
The guy killed as a direct consequence of his craziness. He did not (probably still does not) understand that
he killed his parents. He saved them from the demons. He suffered from a defect of reason so as not to be able to tell right from wrong ... (that's called the M'Naughton Rule) and is the test for insanity in many jurisdictions.
Some jurisdictions also have a notion of Guilty but Mentally Ill. This is often a plea agreement ... the individual pleads guilty, gets mental health treatment, but still serves the term of his sentence. If he's still nuts at the end of it, as far as I am aware the mental health treatment can continue. If the mental illness clears, the jail term stands. I suppose a jury could find for that as well.
NGRIs, since the patient is presumably curable can get released before the criminal penalties would have ended.
Here's another classic Not Guilty By Reason of Insanity. Despite the article being from the Carolinas, the crime happened up near Pottstown.
Richard Greist.
He's been petitioning for expansion of privileges, not unlike John Hinkley, Jr.