Sayyiduna Anas (may Allah be pleased with him) said: Two men sneezed in the presence of the Prophet (peace and blessings be upon him), so the Prophet (peace and blessings be upon him) responded, or it is said that the Prophet (peace and blessings be upon him) said "Yarhamukallah" to one of them and did not respond to the other. It was said to him: O Messenger of Allah! You responded to this one but did not respond to the other? He said: "He said 'Alhamdulillah' and the other did not say 'Alhamdulillah'." Imam Darimi (may Allah have mercy on him) said: Sulaiman is Tamimi.
Hadith Referenceسنن دارمي / من كتاب الاستئذان / 2695
Hadith Gradingتحقیق (حسین سلیم أسد الدارانی):إسناده صحيح والحديث متفق عليه، [مكتبه الشامله نمبر: 2702]
Hadith Takhrijاس روایت کی سند صحیح اور حدیث متفق علیہ ہے۔ دیکھئے: [بخاري 6221] ، [مسلم 2991] ، [أبوداؤد 5039] ، [ترمذي 2742] ، [ابن ماجه 3713] ، [أبويعلی 4060] ، [ابن حبان 600] ، [الحميدي 1242]
Brief Explanation
(Commentary on Hadiths 2693 to 2695)
That is, whoever said «الحمد للّٰه», his response was given with «يرحمك اللّٰه», and whoever did not say «الحمد للّٰه», this supplication was not given to him.
Therefore, the one who sneezes should definitely say «الحمد للّٰه», because sneezing brings alertness and clears the mind, thus it becomes obligatory to express gratitude to Allah Ta'ala for it. And for the one who does not say «الحمد للّٰه», it is not correct to say «يرحمك اللّٰه» for him.