How did Peter know that it was Moses and Elijah having never seen either of them before?
"Some eight days after these sayings, He took along Peter and John and James, and went up on the mountain to pray. And while He was praying, the appearance of His face became different, and His clothing became white and gleaming. And behold, two men were talking with Him; and they were Moses and Elijah, who, appearing in glory, were speaking of His departure which He was about to accomplish at Jerusalem. Now Peter and his companions had been overcome with sleep; but when they were fully awake, they saw His glory and the two men standing with Him. And as these were leaving Him, Peter said to Jesus, "Master, it is good for us to be here; let us make three tabernacles: one for You, and one for Moses, and one for Elijah" --not realizing what he was saying. While he was saying this, a cloud formed and began to overshadow them; and they were afraid as they entered the cloud. Then a voice came out of the cloud, saying, "This is My Son, My Chosen One; listen to Him!" And when the voice had spoken, Jesus was found alone. And they kept silent, and reported to no one in those days any of the things which they had seen" (Luke 9:28-36).
None of the accounts tells us how the disciples recognized Moses and Elijah. For all we know, Jesus could have introduced them or they could have overheard Jesus using their names.
What this passage does indicate is that after we die and move to the spiritual realm, we are still able to distinguish and recognize each other.