Let the Bible interpret itself.
A few verses later:
And as they came down from the mountain, Jesus charged them, saying, Tell the vision to no man, until the Son of man be risen again from the dead. — Matthew 17:9
Notice that Jesus called the incident a "vision".
The Greek word ὅραμα "horama" is frequently used to indicate a spiritual event, for example it appears 11 times in Acts, such as:
- Acts 9:10 "Said the Lord in a vision"
- 10:17 "Now while Peter doubted in himself what this vision which he had seen should mean"
- 10:19 "While Peter thought on the vision, the Spirit said unto him, Behold, three men seek thee"
- 11:5 "I was in the city of Joppa praying: and in a trance I saw a vision"
- 16:9 "And a vision appeared to Paul in the night"
- 18:9 "Then spake the Lord to Paul in the night by a vision"
It wasn't real. No more than other visions that Peter experienced, such as Peter's vision of the unclean animals:
Now while Peter doubted in himself what this vision which he had seen should mean, … — Acts 10:17
Moses and Elijah weren't really there (how would Peter have known what they look like anyway?), they had long been dead and buried, awaiting the resurrection.
Perhaps this vision was of a conversation between Jesus, Moses, and Elijah that will take place during the Millennium, when the resurrected saints rule and teach along with Jesus in the Kingdom.
What Peter saw was a real event, but it didn't happen there and then, two thousand years ago; he was simply privileged enough to be able to experience a vision of an event that still hasn't happened.
Like John's own vision of the Millennium, recorded in Revelation, what Peter saw seemed very real at the time.
This also accounts for the comments in the other Gospel stories of this incident where Peter suggests putting them up for the night, not yet realizing how inappropriate that suggestion was:
And it came to pass, as they departed from him, Peter said unto Jesus, Master, it is good for us to be here: and let us make three tabernacles; one for thee, and one for Moses, and one for Elias: not knowing what he said. — Luke 9:33