What is the source or origin of the custom to kiss a Mezuzah?
Since the mezuzah serves as a reminder of the Oneness of Hashem's Name, therefore, when leaving the house and when entering you should kiss the mezuzah.
The Rama to Yoreh Deah 285:2, however, mentions that one should just put his hand on the Mezuza. (As a side note, the Lubavitcher Rebbe used to put his hand on the Mezuza without kissing it).
So they [the Romans] took hold of him; and as they were walking on he saw the mezuzah which was fixed on the door-frame and he placed his hand on it saying to them: 'Now what is this?' and they replied: 'You tell us then.' Said he, 'According to universal custom, the mortal king dwells within, and his servants keep guard on him without; but [in the case of] the Holy One, blessed be He, it is His servants who dwell within whilst He keeps guard on them from without; as it is said: The Lord shall guard thy going out and thy coming in from this time forth and for evermore.' (Psalms 121:8)
Onkelos the son of Kalonymus became a proselyte. The emperor sent a contingent of Roman [soldiers] to pursue him, but he enticed them by [citing] Scriptural verses, and they converted to Judaism. The Emperor then sent another Roman cohort, instructing them not to say anything to him. As they were about to take him into custody, he said to them: “Let me tell you . . . ,” and they too converted. He [the emperor] dispatched another cohort and ordered them not to engage in any conversation with Onkelos. As they seized him and were walking, Onkelos saw the mezuzah affixed to the doorway. He placed his hand on it and asked them, “What is this?” They said, “You tell us.” Onkelos replied, “The universal custom is a mortal king dwells within and his servants keep guard over him from without; but with the Holy One, Blessed be He, His servants dwell within while He keeps guard over them from without, as it says, ‘Hashem yishmor tzetcha u’vo’echa me’atah v’ad olam, The Lord will guard your goings and your comings, from now and forever’” (Psalms 121:8). They too converted to Judaism. He [the emperor] sent for him no more.
However the first source that states that one should engage in this practice is the Arizal