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.