Sayyiduna Abdullah bin Mas'ud (may Allah be pleased with him) said: "When you are behind the imam, do not bow before he bows, do not prostrate before he prostrates, and do not raise your head before him. Then when the imam has finished and neither gets up nor turns, and you have some need, go and leave him, for your prayer has been completed."
Hadith Referenceالمعجم الكبير للطبراني / باب العين / 9308