Mishkat al-Masabih 466
Nafi' said:
I accompanied Ibn ‘Umar who wanted to relieve himself. After doing so, in the course of his talk that day he said that a man going along a street met God’s messenger when he came out of the privy, or had passed water, and saluted him without receiving any response. Then when the man was almost going out of sight in the street God’s messenger struck his hands on a wall, wiped his face with them, struck once more and wiped his forearms, then responded to the man’s salutation, saying, “The only thing which prevented me from responding to your salutation was that I was not in a state of purity.”

Abu Dawud transmitted it.
وَعَنْ نَافِعٍ قَالَ: انْطَلَقْتُ مَعَ ابْنِ عُمَرَ فِي حَاجَة إِلَى ابْن عَبَّاس فَقَضَى ابْنُ عُمَرَ حَاجَتَهُ وَكَانَ مِنْ حَدِيثِهِ يَوْمَئِذٍ أَنْ قَالَ مَرَّ رَجُلٌ فِي سِكَّةٍ مِنَ السِّكَكِ فَلَقِيَ رَسُولَ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ وَقَدْ خَرَجَ مِنْ غَائِطٍ أَوْ بَوْلٍ فَسَلَّمَ عَلَيْهِ فَلَمْ يَرُدَّ عَلَيْهِ حَتَّى كَادَ الرَّجُلُ أَنْ يَتَوَارَى فِي السِّكَّةِ ضَرَبَ رَسُولُ اللَّهِ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ بِيَدَيْهِ عَلَى الْحَائِطِ وَمَسَحَ بِهِمَا وَجْهَهُ ثُمَّ ضَرَبَ ضَرْبَةً أُخْرَى فَمَسَحَ ذِرَاعَيْهِ ثُمَّ رَدَّ عَلَى الرَّجُلِ السَّلَامَ وَقَالَ: «إِنَّهُ لَمْ يَمْنَعْنِي أَنْ أَرُدَّ عَلَيْكَ السَّلَامَ إِلَّا أَنِّي لَمْ أَكُنْ على طهر» . رَوَاهُ أَبُو دَاوُد