Sayyiduna Abu Sa'id al-Khudri (may Allah be pleased with him) says: When Khaybar was conquered, we came upon some vegetables that included garlic, and we ate a great deal of it, as we were also hungry. Then we went to the mosque, and the Prophet (peace and blessings be upon him) noticed the smell of garlic. He (peace and blessings be upon him) said: "Whoever eats of this foul plant, let him not come near the mosque." So the people began to say: It has been made unlawful, it has been made unlawful. This reached the Prophet (peace and blessings be upon him), and he (peace and blessings be upon him) said: "O people, what Allah has made lawful is not unlawful. But I dislike it because of its smell."
Hadith Referenceالسنن الكبرى للبيهقي / كتاب الصلاة / 5060