The Prophet ﷺ came out to lead the prayer, and when he had pronounced the opening takbir, he turned away and gestured to them to remain as they were. He then left, performed a full bath, and returned with water still dripping from his head. He led them in prayer, and when he had finished he said, "I was in a state of major ritual impurity and forgot to bathe."
حدثنا وكيع قال حدثنا أسامة بن زيد عن عبد الله بن يزيد مولى الأسود بن سفيان عن ابن ثوبان
أن النبي ﷺ خرج إلى الصلاة فلما كبر انصرف وأومأ إليهم أي كما أنتم ثم خرج فاغتسل ثم جاء ورأسه يقطر فصلى بهم فلما صلى قال إني كنت جنبا فنسيت أن أغتسل
No references are available.
Its isnad is sahih. Usama bin Zayd is al-Laythi. Ibn Thawban is Muhammad bin Abdul-Rahman bin Thawban, attributed to his grandfather. The hadith appears in Bukhari 1/77, in the chapter on ghusl, when a man remembers while in the masjid that he is in a state of janabah, and in Abu Dawud 1/61, number 235, in the chapter on purification.