How the Prophet (blessings and peace of Allah be upon him) suffered the agonies of death
Question

Why was the Prophet (blessings and peace of Allah be upon him) not spared the agonies of death?
Praise be to Allah.Al-Bukhari (6510) narrated that ‘A’ishah (may Allah be pleased with her) said: The Messenger of Allah (blessings and peace of Allah be upon him) had near him a vessel in which there was water. He started dipping his hands in the water, then wiping his face with them and saying: “La ilaha illa Allah; indeed death has agonies.” Then he held his hand up and started saying: “With those on high,” until his soul was taken and his hand dropped.
Al-Hakim (119) narrated from Abu Sa‘id al-Khudri that he went to visit the Messenger of Allah (blessings and peace of Allah be upon him) when he had a fever, with a blanket over him. He put his hand on it and felt the heat through the blanket, and he said: How severe is the heat of your fever, O Messenger of Allah! The Messenger of Allah (sa) said: “We [prophets] are like that; our trials are severe and our reward will be multiplied.” Classed as sahih by al-Albani in Sahih at-Targhib (3403).
The Prophet (blessings and peace of Allah be upon him) suffered the agonies of death, and that was so that he would bear them with patience and thus rise in status with Allah, may He be exalted, and attain the reward of the patient:
{Indeed, the patient will be given their reward without account} [az-Zumar 39:10]
{So be patient, [O Muhammad], as were those of determination among the messengers} [al-Ahqaf 46:35].
It was inevitable that he (blessings and peace of Allah be upon him) would be tested with sickness, his enemies’ offending and harming him and all types of trials, including the agonies of death, so that his patience would be made manifest, and so that he would set an example of patience.
Shaykh Ibn Baz (may Allah have mercy on him) said:
Allah, may He be glorified and exalted, tests His slaves with ease and hardship, privation and prosperity, and He may test them thereby in order to raise their status and renown, and multiply the reward for their good deeds, as He does for the prophets and messengers (peace be upon them) and the righteous among the slaves of Allah."( Majmu‘ Fatawa Ibn Baz  4/370).
So whatever befell the Prophet (blessings and peace of Allah be upon him) of the agonies of death, severe fever and other trials was only in order to raise him in status and multiply his reward, and so that his ummah might take him as an example.
See also the answers to questions no. 13210 and 72265 .