Why 5 Takbir on the dead body

حَدَّثَنا مُحَمَّدِ بْنِ الحَسَن بْنِ أَحْمَدِ بْنِ الوَلِيد رَضِىَ اللهُ عَنْهُ قالَ: حَدَّثَنا مُحَمَّدِ بْنِ الحَسَن الصَفَّار، عَنْ مُحَمَّدِ بْنِ عِيسَى، عَنِ الحَسَن النَّضر قالَ: قُلْتُ لِلرِّضا عَلَيْهِ السَّلامُ: مَا الْعِلَّةُ فِي التَّكْبِيرِ عَلَى الْمَيِّتِ خَمْسُ تَكْبِيرَاتٍ قُلْتُ رَوَوْا أَنَّهَا قَدِ اشْتُقَّتْ مِنْ خَمْسِ صَلَوَاتٍ فَقَالَ هَذَا ظَاهِرُ الْحَدِيثِ فَأَمَّا بَاطِنُهُ فَإِنَّ اللَّهَ عَزَّ وَجَلَّ فَرَضَ عَلَى الْعِبَادِ خَمْسَ فَرَائِضَ الصَّـلاةَ وَالزَّكَـاةَ وَالصِّيَـامَ وَالْحَجَّ وَالْوَلايَةَ فَجَعَلَ لِلْمَيِّتِ مِنْ كُلِّ فَرِيضَـةٍ تَكْبِيرَةً وَاحِدَةً فَمَنْ قَبِلَ الْوَلايَةَ كَبَّرَ خَمْساً وَمَنْ لَمْ يَقْبَلِ الْوَلايَةَ كَبَّرَ أَرْبَعاً فَمِنْ أَجْلِ ذَلِكَ تُكَبِّرُونَ خَمْساً وَمَنْ خَالَفَكُمْ يُكَبِّرُ أَرْبَعاً

Muhammad ibn Al-Hassan ibn Ahmad ibn al-Waleed - may God be pleased with him - narrated that Muhammad ibn Al-Hassan al-Saffar quoted on the authority of Muhammad ibn Isa that Al-Hassan al-Nadhr asked Al-Reza (as),

"What is the reason they say the phrase God is the Greatest five times over a person’s corpse?"

He (as) said, “On the surface it seems that this is because it has been taken from the five daily prayers. However, there is another reason for that. Indeed the Honorable the Exalted God has made it incumbent upon His servants to perform five obligatory deeds: praying, paying the alms-tax, fasting, going on the Hajj pilgrimage, and accepting the Mastery (Wilayah). Therefore, He has established saying God is the Greatest once for each of the obligatory deeds.

If he has accepted the Wilayah, then it should be said five times. However, if he has not accepted it, the phrase God is the Greatest should be said only four times. That is why when one of you (the Shiites) die, the phrase is said five times. However, when one who disagrees with you (i.e. is not one of the Shiites) dies, the phrase is said only four times.

[Source: Uyun Al Akhbar Al Reza Vol. 2 Ch. 32 Hadees. 20]

The above narration is also verified by another narration in which the Prophet Muhammad (saww) used to recite only 4 Takbir's in the Prayer of a dead hypocrite:

عَبْدِ اللَّهِ ع يَقُولُ كَانَ رَسُولُ اللَّهِ ص إِذَا صَلَّى عَلَى مَيِّتٍ كَبَّرَ وَ تَشَهَّدَ ثُمَّ كَبَّرَ وَ صَلَّى عَلَى الْأَنْبِيَاءِ وَ دَعَا ثُمَّ كَبَّرَ وَ دَعَا لِلْمُؤْمِنِينَ ثُمَّ كَبَّرَ الرَّابِعَةَ وَ دَعَا للمَيِّتِ ثُمَّ كَبَّرَ الْخَامِسَةَ وَ انْصَرَفَ فَلَمَّا نَهَاهُ اللَّهُ عَزَّ وَ جَلَّ عَنِ الصَّلَاةِ عَلَى الْمُنَافِقِينَ كَبَّرَ وَ تَشَهَّدَ ثُمَّ كَبَّرَ وَ صَلَّى عَلَى النَّبِيِّينَ ثُمَّ كَبَّرَ وَ دَعَا لِلْمُؤْمِنِينَ ثُمَّ كَبَّرَ الرَّابِعَةَ وَ انْصَرَفَ وَ لَمْ يَدْعُ للمَيِّتِ

Muhammed Ibn Yaqub narrated from Ali Ibn Ibrahim, he narrated from his father, he quoted from Muhammed Ibn Muhajir, he narrated from his mother Umme Salama, she said that; I heard Aba Abdillah (as) mentioning that;

“Whenever Holy Prophet (saww) offered Namaz for deceased, He said takbir and recited shahadat, then said takbir and send blessing to prophets (as) and prayed, then said takbir and prayed for the believers, then said fourth takbir and prayed for the deceased, then said fifth takbir and completed the namaz. Then Allah (swt) asked not to offer prayers for hypocrites, he said one takbir then recited shahadat, then said another takbir and send blessings for the prophets (as) then said takbir and prayed for believers then said fourth takbir and finished without praying for the dead one.

[Source: Wasail-ush-Shia Vol: 3 Page 60 Ch: Manner of Offerening prayers for deceased]