The bathroom door isn't really locked when you're inside. You can unlock Lavatory doors from the outside. Because it allows flight attendants quick access to locked lavatories in case of emergencies. In other instances, it lets the cabin crew restrict access to bathrooms during takeoff and landing.