There are two possible things happening here:
1) there’s a problem (low oil pressure most likely in this case) – as soon as you release the key, the alarm kicks in and stops the engine (the key overrides all safety switches).
2) There’s an electrical problem – loose connection, bad contact, etc. It could be in anywhere, really.
Does the control panel indicate any alarms, or anything like that?
Hope this helps – please share more info if you find out anything.