This seems to be an area of law that is poorly understood; in media, it is not uncommon to see mental incapacity framed as a way to "get away with" a criminal action. Underlying this misconception is a lack of understanding of what constitutes a crime: specifically, a failure to differentiate between the required physical and mental components of a crime.
While a physical action (the actus reus) is an essential element of a crime, alone it does not constitute a crime. It if did, then genuine accidents* would have to be considered criminal. To be criminal, an act must also have mens rea - roughly, a guilty mind. There are different thresholds for this. Some crimes, homicide of the first degree, for example, require intent; in some instances, knowledge, just an awareness of the action, is sufficient; finally, some crimes require only recklessness, a failure to exercise reasonable caution given the likely outcome of an action. For any act to be criminally culpable, the mens rea must be present.
Mental incapacity in this context describes a condition whereby an accused person is unable to form the mens rea required for the charge being brought. This ruling can be made when a court psychologist brings findings showing that an impairment or condition experienced by the accused would have been sufficient to prevent them from, for example, foreseeing the likely outcome of an action. The standard for this is high. Generally, even severely impaired people are seen as capable of general intent. When it happens, the accused person is found innocent because they genuinely are. The crime has a required component that was not present and there is no more criminal culpability than in the case of genuine accident.
*That is to say, discounting "accidents" caused by recklessness.