All perfect praise be to Allah, The Lord of the Worlds. I testify that there is none worthy of worship except Allah, and that Muhammad is His slave and Messenger.
There is no doubt that it is only Allah Who has the knowledge of the Unseen and Allah Almighty revealed whatever He Wished of this Unseen to His Prophet so that he would establish the evidence and prove the truthfulness of his Message to people in addition to other reasons based on His wisdom.
Allah Says (what means): {[He is] Knower of the unseen, and He does not disclose His [knowledge of the] unseen to anyone. Except whom He has approved of messengers.} [Quran 72:26-27]
Ibn Katheer said about the verse 27: "This is similar to Allah's statement (which means), {…and they encompass not a thing of His knowledge except for what He wills.} [Quran 2:255] Similarly, Allah says here that He knows the unseen and the seen and that no one of His creation can attain any of His knowledge except that which Allah allows him to have. Thus, Allah Says (what means), {and He does not disclose His [knowledge of the] unseen to anyone. Except whom He has approved of messengers.}; this includes the angelic Messenger and the human Messenger." [End of quote]
Ibn Al-Jawzi said in Zaad Al-Maseer, when interpreting the above verse:
"The knowledge of the Unseen is exclusive to Allah, so He does not disclose of His knowledge to anyone, except whom He has approved from amongst His messengers. This is because one of the evidences of the truthfulness of the Prophets is informing them of the Unseen. The meaning is that “Whomever Allah is pleased with to send the Message to him, He informs him of whatever He Wishes of His Unseen.” [End of quote]
For more benefit, please refer to Fataawa 262183, have knowledge of the Unseen?">81730, 84215 and 238015.
Allah knows best.