The basic concept here is that a person cannot be found guilty of a crime if the person was, for whatever reason, incapable of understanding that the act they were committing was illegal. Such a person would not have the appropriate mental state to be found guilty of a crime.
Legally speaking, there is the idea that a person must reasonably be able to know their conduct was illegal. They must be able to understand the nature of their act (that it is illegal) and its consequences (that it can result in them being punished). People who are too young or who are otherwise impaired (such as by mental illness) are deemed to be incapable of understanding these things.
This points to a basic concept -- that an action is only criminal if the person committing it is capable of understanding that it was criminal. It is not only the act that makes the crime, it is the act plus the mental state of the actor.