Jesus described it as “a vision”
The most likely explanation of the appearance of Elijah and Moses’ appearance with Jesus seen by three of the disciples is the one given by Jesus himself:
And as they were coming down the mountain, Jesus commanded them, “Tell no one the vision, until the Son of Man is raised from the dead.” (Matthew 17:9 ESV)
Although the word ‘vision’ (Greek orama) used by Jesus is here translated “what you have seen” in some English Bibles (notably the NIV), the Greek word used here does usually mean more than simply “what you have seen”, and the NIV is probably incorrect in this case.
All the other New Testament uses of this word, orama, mean a miraculous vision, not normal sight: Moses’s vision of the burning bush (Acts 7:31), Ananias’ vision of Jesus (Acts 9:10), Paul’s vision of Ananias (Acts 9:12), Peter’s vision of an angel and sheet of unclean food (Acts 10:3, 17, 19, 11:15), Peter mistakenly thinking that a real event was only a vision (Act 12:9), Paul’s vision of a man of Macedonia (Acts 16:9, 10), and Paul’s night vision of Jesus (Acts 18:9). The same is also true of the usage of this word in other Jewish Greek texts.
The parallel account in the Gospel of Mark records instead that “And as they were coming down the mountain, he charged them to tell no one what they had seen” (Mark 9:9), but the sense of a ‘vision’ is still implied by the light and the way Elijah and Moses appeared and disappeared.
Elijah and Moses were dead, not in the spirit world
The more important thing to be considered here is Bible teaching on death, the unconscious state of the dead, and the reservation of judgment and salvation of the dead till the resurrection when Christ returns. This is confirmed in that both men are mentioned in the list of the dead who have “not yet received what was promised” in Hebrews (11:39-40).
The one verse which has probably contributed to readers who otherwise would have no difficulty accepting the normal teaching on Moses and Elijah having thoughts about God having somehow temporarily raised these two men is found in Luke:
And behold, two men were talking with him, Moses and Elijah, who appeared in glory and spoke of his departure, which he was about to accomplish at Jerusalem. (Luke 9:30-31)
It’s one thing to record, as Matthew and Mark do, these two men appearing in a vision, but it’s quite another for Luke to record the overheard content of the conversation with Jesus. The conversation as reported also makes the vision likely to also have had some value to Jesus himself, rather than just being a fulfillment of the promise which Jesus had made eight days earlier “But I tell you truly, there are some standing here who will not taste death until they see the kingdom of God.” (Luke 9:27).
The conflict between the normal Bible teaching that “the dead know nothing” (Ecclesiastes 9:5, cf Psalms etc) and Luke’s record of Jesus actually discussing his departure with Moses and Elijah has led to a number of ideas including the idea that Christ actually walked through a time window to the future kingdom age when Moses and Elijah were themselves raised and in the kingdom. But it has to be said that this idea probably owes more to H.G. Wells and Star Trek than to similar visions of the future kingdom age found in the Old Testament. A more biblical explanation might be found in Jesus’ statement in John that “Your father Abraham rejoiced at the thought of seeing my day; he saw it and was glad.” (John 8:56) In offering up to John, James and Peter a vision of the kingdom where Christ would receive homage from Moses and Elijah the vision was not substantially different from visions of the future Messiah king seen by Isaiah, Ezekiel and Daniel, nor of those that John of Patmos would later include in the Book of Revelation.
As to the why of why Jesus would arrange to include Moses and Elijah in this vision, the context goes back to the previous verses in Matthew:
Take Up Your Cross and Follow Jesus
24 Then Jesus told his disciples, “If anyone would come after me, let him deny himself and take up his cross and follow me. 25 For whoever would save his life will lose it, but whoever loses his life for my sake will find it. 26 For what will it profit a man if he gains the whole world and forfeits his soul? Or what shall a man give in return for his soul? 27 For the Son of Man is going to come with his angels in the glory of his Father, and then he will repay each person according to what he has done. 28 Truly, I say to you, there are some standing here who will not taste death until they see the Son of Man coming in his kingdom.” (Matthew 16:24-28)
The appearance of Moses and Elijah in the vision is not fundamentally about strengthening Jesus, even if that implication does appear to be present in Luke, but about the guarantee Jesus had given the twelve. Moses and Elijah do not appear for Jesus to give them honour, but the reverse: Moses and Elijah appear in order to represent the repayment of those faithful in the Old Covenant when Christ raises them also from the dead.