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.
The fact that the Hajj of the Prophet took place in the tenth year of Hijrah and him giving the Farewell Sermon in it was all by revelation from Allah.
Allah informed His Prophet that his appointed time had come near and that his death was nigh.
Ibn ‘Abbaas narrated:
“‘Umar used to admit me (into his house) along with the old men who had fought in the Battle of Badr. Some of them said (to ‘Umar), ‘Why do you allow this young man to enter with us, while we have sons of his own age?’ ‘Umar said, ‘You know what person he is.’ One day, `Umar called them and called me along with them, and I thought that he called me on that day to show them something about me (i.e. my knowledge). `Umar asked them, ‘What do you say about (the Soorah): {When the victory of Allah has come and the conquest, and you see the people entering into the religion of Allah in multitudes, then exalt [Him] with praise of your Lord and ask forgiveness of Him. Indeed, He is ever Accepting of repentance.} [Quran 110:1-3]’ Some of them replied, ‘We are ordered to praise Allah and repent to Him if we are helped and granted victory.’ Some said, ‘We do not know.’ Others kept quiet. ‘Umar then said to me, ‘Do you say the same?’ I said, ‘No.’ ‘Umar said, ‘What do you say then?’ I said, ‘This indicated the approach of the death of Allah's Messenger of which Allah informed him. When the victory of Allah has come and the conquest, i.e. the Conquest of Makkah, that will be the sign of the approach of your death (Prophet's death); then exalt [Him] with praise of your Lord and ask forgiveness of Him. Indeed, He is ever accepting of repentance.}’ On that, ‘Umar said, ‘I do not know about it anything other than what you know.’”
There were several ahaadeeth in which the Prophet hinted about his death to his Companions, among which is his saying during his Farewell Hajj, “I might not meet you after this year of mine.”
The wording of the hadeeth by Muslim reads, “Take your rituals (from me), as I do not know if I will perform Hajj again after this very Hajj.”
An-Nawawi said, “The hadeeth indicates giving them farewell and informing them about the approach of his death, and he urged them to learn (the rituals) from him and seize the opportunity of being with him and learn the matters of the religion (from him); it is for this reason that it was called the Farewell Hajj.”
The wording of Ibn Maajah reads, “Then he bid farewell to the people; so they said, ‘This is the Farewell Hajj.’”
Among other ahaadeeth is also that the Prophet sent Mu’aath to Yemen. He said to him, “O Mu’aath, you might not meet me after this year of mine, and you may pass by this mosque of mine and my grave.” [Ahmad]
The Prophet also said, “O people, certainly I am a human being. I am about to receive a messenger (the angel of death) from my Lord, and I will respond...” [Muslim]
Moreover, Abu Sa'eed Al-Khudri narrated:
“The Prophet sat on the pulpit and said, ‘Allah has given one of His slaves the choice of receiving from the splendor and luxury of this worldly life whatever he likes or to accept the good (of the Hereafter) which is with Allah, so he has chosen that good which is with Allah.’ On that, Abu Bakr wept and said, ‘We ransom you with our fathers and mothers, O Prophet of Allah.’ We became astonished at this. The people said, ‘Look at this old man! Allah's Messenger talks about a slave of Allah to whom He has given the option to choose either the splendor of this worldly life or the good which is with Him, while he says, ‘We ransom you with our fathers and mothers.’ But it was Allah's Messenger who had been given the option, and Abu Bakr knew it better than us.”
Allah knows best.