The dua to recite after leaving the toilet by the Prophet (ﷺ). Like other duas, this dua helps gain Allah's (سُبْحَانَهُ وَتَعَالَى) blessings and is a way of offering one's gratitude to Him. Moreover, duas are a Sunnah of the Prophet (ﷺ), and following his Sunnah can help believers receive tremendous rewards in this life and the hereafter. Following the Prophet's Sunnah is also a way of getting close to Allah (سُبْحَانَهُ وَتَعَالَى).
It has been narrated by Aisha (رَضِيَ ٱللَّٰهُ عَنْهَا) that, "When the Prophet would exit the toilet, he would say: 'Ghufranak.'" – (Jami at-Tirmidhi 7). There has also been a narration that narrates another dua before exiting the lavatory; it was reported by Anas bin Malik (رَضِيَ ٱللَّٰهُ عَنْهُ) and goes as follows: "Whenever the Prophet exited the toilet, he would say: 'Al-hamdu lillahilladhi adhhaba 'annial-adha wa 'afani (Praise is to Allah Who has relieved me of impurity and given me good health).'" – (Sunan Ibn Majah 301).
These two Ahadith signify the fact that to recite dua after leaving the lavatory for protection and blessings from Allah (سُبْحَانَهُ وَتَعَالَى) is Sunnah of the Prophet (ﷺ) and every Muslim is recommended to follow it. The dua should be recited after one exits the lavatory, whether you leave it after relieving yourself or after other activities such as brushing one's teeth or taking a bath. However, make sure to exit the toilet by putting your right foot out first, as it is a recommended etiquette.
There is no specific etiquette to be followed for reciting this Dua. You can recite it after exiting the lavatory once you're done relieving yourself and have cleaned yourself properly. Or you can recite it immediately after leaving the lavatory once you've taken a shower, brushed your teeth, or cleaned up the area. The Prophet (ﷺ) used to recite it as soon as he exited the lavatory. So, it is recommended to do the same.
Reciting the dua to be recited after leaving the toilet has the following benefits: