Any person who has lawfully entered a house for the purpose of making an arrest therein, may break open any door or window if he has been detained therein, when necessary for the purpose of liberating himself, and a peace officer may do the same to free a person who has lawfully entered a building with the purpose of making an arrest and has been detained therein.