Answered by Shaykh Sohail Hanif, SunniPath Academy Teacher
I prayed at the local mosque, as I entered I saw the imam A from behind thinking it to be someone I knew and started the prayer behind him. Throughout the prayer I thought it was imam A. After the prayer had finished the imam was in fact someone different. Was my prayer valid behind imam B, with me thinking that it was imam A leading the prayer?
It is a condition for the validity of a group prayer that one intends to follow ones imam. It is not a condition that one specifies the identity of the imam. Therefore whether it is imam A that leads the prayer or imam Z it makes no difference as long as one intended to follow the imam. (al-Durr al-Mukhtar, 1:285, (published with Radd al-Muhtar) Dar Li Ihya al-Turath al-Arabi)