Matthew wrote about the "Temptation of Jesus" in third person as if he were there or as if he had very detailed information about what happened. I suppose that Jesus could have related the events to him, and then 40 years later he decided to write his gospel.
The simpler explanation is "he made it up".
This also explains his "flat earth" point of view:
"the devil took him to a very high mountain and showed him all the kingdoms of the world and their glory"
The Temptation of Jesus
1Then Jesus was led up by the Spirit into the wilderness to be tempted by the devil. 2And after fasting forty days and forty nights, he was hungry. 3And the tempter came and said to him, “If you are the Son of God, command these stones to become loaves of bread.” 4But he answered, “It is written,
“‘Man shall not live by bread alone,
but by every word that comes from the mouth of God.’”
5Then the devil took him to the holy city and set him on the pinnacle of the temple 6and said to him, “If you are the Son of God, throw yourself down, for it is written,
“‘He will command his angels concerning you,’
and
“‘On their hands they will bear you up,
lest you strike your foot against a stone.’”
7Jesus said to him, “Again it is written, ‘You shall not put the Lord your God to the test.’” 8Again, the devil took him to a very high mountain and showed him all the kingdoms of the world and their glory. 9And he said to him, “All these I will give you, if you will fall down and worship me.” 10Then Jesus said to him, “Be gone, Satan! For it is written,
“‘You shall worship the Lord your God
and him only shall you serve.’”
11Then the devil left him, and behold, angels came and were ministering to him.