The primary source for the orientation of the words of the Mezuzah is a cryptic Yerushalmi (Megillah 4:12) which states:
ר' זעורא בשם שמואל צריך שיהא שמע שלה רואה את הפתח
R' Ze'ura [said] in the name of Shemuel: [the Mezuzah] needs to be such that its 'Shema' sees the opening.
For Rashi (Menachot 33a sv כמין), Rambam (Hilchot Mezuzah 5:8), and the Mordechai (Menachot 961) who understand another cryptic Gemara (Bavli Menachot 33a) which requires orienting the Mezuzah כמין נגר 'like a [carpenter?]' to mean vertically, the Yerushalmi is interpreted to meant that the word Shema should be on the outside of the rolled parchment, not against the doorpost. (Everyone agrees that the direction of rolling is from left to right (Menachot 31b) and thus the word Shema is not in the middle of the tube.)
For Rabbeinu Tam (see Tosfot Menachot 33a sv Hah), who understands that the Mezuzah should be placed horizontally, the Yerushalmi is interpreted to mean that not just the word 'Shema' but the top of the writing (ie the whole first line) should be towards the inside of the house, because the Gemara earlier (Menachot 32b) ruled that the in a deep doorway, the Mezuzah should be placed in the Tefach closest to the outside. (For the vertical opinion, it is obvious that the top of the writing should be at the top, and not to have the writing upside down.)
Thus when following one of the two "compromise" opinions (such as having the mezuzah on a slant or Maharam of Rothenburg's original compromise: placing it bent in an L shape), the Terumat haDeshen (#52) concludes that the Shema should be tilted inward (or in the case of an L the bottom leg should be bent outward) to satisfy Rabbeinu Tam's understanding of the Yerushalmi. (It's worth noting that the Terumat haDeshen claims he wrote this Teshuva to oppose the practice of many Talmidei Chachamim who had the tops of their Mezuzot angled outwards.)
The Terumat haDeshen also suggests another reason for having the top inwards. He compares the case to that of Tefillin, which the Talmud (Menachot 34b) requires to be placed in order as if one was reading them. (Speaking of cryptic gemaras, feel free to see a famous Machloket between Rashi and Rabbeinu Tam over there :D). The Terumat haDeshen suggests that the reason the Shema in a Mezuzah is placed on the inside is so that someone entering the door (and our reference point for Mezuzah is always someone entering (eg. placing on the right side)) would be able to read the Mezuzah (if it was unrolled). If the top of the writing were tilted towards the outside, someone entering would have to contort themselves to read it (yes, the Terumat haDeshen actually says that). Thus the Mezuzah may only be placed between vertical and top-inwards horizontal, independent of if you hold vertical or horizontal is the ideal.