It was taught in a baraita in Bava Metzia (Bava Metzia 102a): One who leases a house to his fellow—the tenant must provide a mezuzah for it. And when he leaves—he may not take it in his hand and leave. And it happened that one took it in his hand and left, and he buried his wife and his two sons. And even though regarding tzitzit we hold: one may untie them from one garment for another garment, mezuzah is different because it causes damaging spirits to enter (see Beit Yosef). However, he may claim their value from the one who enters to dwell within it. And even if this one who enters does not want to pay, nevertheless it is forbidden for him to take them. But in a case where he leases it to a gentile—he takes the mezuzot. And so too if he leased a house from him and fixed mezuzot there, when he leaves it—he may take it. Unless he leases it further to another Israelite, he may take their value from that Israelite who enters, and when he leaves—he takes them. And there is one who wishes to permit taking the mezuzot even from the house of an Israelite when he leaves, if he is unable to obtain mezuzot for the place where he is entering (Pitchei Teshuvah in the name of Birkei Yosef). And it requires further study. And a non-Jew who requests a mezuzah from an Israelite to give it to him so that he may fix it to his entrance—he should not give it to him, for he is not commanded in this mitzvah. But if there is a concern of enmity, and that harm will reach him from this—it is permitted. (And proof is from the Yerushalmi, first chapter of Pe'ah, that Rabbi sent a mezuzah to Artaban. And the Pnei Moshe explained that he was an Israelite. And it is not so, as is proven in the She'iltot, Parshat Ekev, see there. And Rabbi was certain that he would honor it, see there in the She'iltot. And look closely and you will find it simple.) (And see Tosafot Shabbat 22a, beginning with "Rav," in the first resolution.)
תניא בבבא מציעא (בבא מציעא קב א): המשכיר בית לחבירו – על השוכר לעשות לו מזוזה. וכשהוא יוצא – לא יטלנה בידו ויצא. ומעשה באחד שנטלה בידו ויצא, וקבר אשתו ושני בניו. ואף על גב דבציצית קיימא לן: מתירין מבגד לבגד, מזוזה שאני שגורם למזיקים שיבואו (עיין בית יוסף). מיהו יכול לתבוע דמיהן מהנכנס לדור בתוכה. ואף אם זה הנכנס אינו רוצה לשלם, מכל מקום אסור לו ליטלן. אבל במשכירה לעובד כוכבים – נוטל המזוזות. וכן אם שכר בית ממנו וקבע שם מזוזות, כשיצא ממנה – יכול ליטלה. אלא אם כן משכירה עוד לישראל אחר, יכול ליטול דמיהם מאותו ישראל הנכנס, וכשהוא יוצא – יטלם. ויש מי שרוצה להתיר ליטול המזוזות גם מבית ישראל כשיצא, אם אין לו להשיג מזוזות למקום שנכנס שם (פתחי תשובה בשם ברכי יוסף). וצריך עיון. ואינו יהודי המבקש מזוזה מישראל ליתנה לו שיקבע בפתחו – לא יתן לו, שהרי אינו מצוה במצוה זו. אך אם יש לחוש משום איבה, ושיגיע לו רעה מזה – מותר. (וראיה מירושלמי פרק קמא דפאה, דרבי שלח מזוזה לארטבון. והפני משה פירש דישראל היה. ואינו כן, כדמוכח בשאלתות פרשת עקב, עיין שם. ורבי היה בטוח שיכבדה, עיין שם בשאילתות. ודייק ותמצא קל.) (ועיין תוספות שבת כב א ד"ה רב בתי' ראשון.)