If your brain damage is so great that either (1) you cannot understand the nature of the proceedings against you; or (2) you are unable to assist in your defense, then you are unfit to proceed and cannot be prosecuted. This does not mean you will be released, at least not right away. People found unfit to proceed are usually turned over to a mental health facility to determine if their fitness will be restored. If their fitness is restored, the prosecution can continue. If not, they have to be released within one year, if lack of fitness is the only reason for holding them.

Otherwise your mental state after you commit the offense (caused by hitting your head or whatever) is irrelevant. What matters is only whether you had had the requisite mental state for the crime when you committed it.