The Gemora cites a braisa: If one rents a house to his fellow, the tenant must provide a mezuzah. And when he leaves, he must not take it with him, unless he rented it from a gentile, in which case, he must remove it when he leaves. And it once happened that a man took the mezuzah away with him, and he buried his wife and two children.
The Gemora asks: Do we relate a story in contradiction of what we had just learned?
Rav Sheishes answers: It refers to the first clause (where he rented from a Jew).
Tosfos writes that there are mazikin - supernatural forces that enter a house bereft of a mezuzah.
The Ritva explains that this punishment was measure for measure. Since he was not concerned about the danger that could befall the future residents (for now, they will not be protected by the mezuzah), he himself suffers, and he is not afforded any protection.
The Kesef Mishnah writes that a mezuzah, which is written correctly, will protect the residents of the house. The protection is not afforded because of the names of Heavenly angels that are written there. And, he concludes, it will only protect a person if it was affixed to the doorpost for the sake of the mitzvah; not if it was placed there solely for protection.
The Maharitz Chayus compares the protection afforded by the mezuzah to the protection that comes from the studying of Torah or the performance of any mitzvah. It would emerge that that there is no greater protection afforded by the mezuzah more than any other mitzvah in the Torah.
Reb Avi Lebovitz points out that seemingly, there will be no protection afforded to a person who is exempt from the mitzvah of mezuzah, and does so anyway. It is not the mezuzah that provides the protection; it is the mitzvah of the mezuzah.
However, the Maharsham cites an opinion of the Shevus Yaakov, who holds that even if one is exempt from placing a mezuzah on a certain doorpost, he may do so for protection purposes, and he would not be called a hedyot.