Yuck, who wants to have poop smell in the rest of the house/apartment?
Locking the door is the quick and easy way to signal that the room is occupied, knocking and/or asking and waiting for an answer would invariably take longer and create uncomfortable situations whenever someone doesn't remember or doesn't have the patience to wait for the answer or is just visiting and assuming that everyone locks the door when they use the bathroom.