What would happen if pilots were locked out of the flight deck?
The flight deck door lock system is normally turned on while on the ground and remains on for the entire flight. Once the system is turned on, the door DOES lock automatically when closed and can only be opened by the pilots in the flight deck. 

If both pilots left the flight deck, the door would lock automatically behind them unless the door lock system was first turned off. There are procedures in place which can allow the door to be unlocked from the cabin in case no one in the flight deck can open it due to incapacitation.

The pilots can also over-ride this unlocking procedure in case they suspect criminal interference or un-authorized entry attempt.  This over-ride feature prevented the Captain of Germanwings 9525 from re-entering the flight deck as the suicidal First Officer deliberately crashed the aircraft killing all on board.