That's not supposed to happen.
In my 2008 Matrix, if I tried to lock the doors with the key in the ignition, the doors would lock then immediately unlock, regardless if I used the mechanical switch or the electrical switch. All modern Toyota vehicles do this.
Sounds like a problem with an alarm perhaps?