The Messenger of Allah ﷺ said: "Whoever has doubt during his prayer, let him prostrate twice." (One of the narrators) Hajjaj said: "After he has said the taslim." (Another of them) Rawh said: "While he is sitting."
أَنَّ رَسُولَ اللَّهِ ﷺ قَالَ مَنْ شَكَّ فِي صَلاَتِهِ فَلْيَسْجُدْ سَجْدَتَيْنِ قَالَ حَجَّاجٌ بَعْدَ مَا يُسَلِّمُ وَقَالَ رَوْحٌ وَهُوَ جَالِسٌ