In the name of Allah, the Beneficent, the Merciful
A Muslim who commits suicide is considered as having committed a major sin, but they do not leave the fold of Islam on account of such sins alone and they are still considered Muslims; therefore, the Muslim community should pray over them.
There is an authentic tradition in which a man at the time of the Prophet, peace and blessings be upon him, had killed himself and the Prophet did not pray over him. Jabir ibn Samurah reported:
أَنَّ رَجُلًا قَتَلَ نَفْسَهُ فَلَمْ يُصَلِّ عَلَيْهِ النَّبِيُّ صَلَّى اللَّهُ عَلَيْهِ وَسَلَّمَ
A man killed himself and the Prophet, peace and blessings be upon him, did not pray over him.
Source: Sunan At-Tirmidhi 1068, Grade Sahih
On the surface, it appears that the ruling is for the Muslims not to pray over such a person, but this tradition is a great example for students to learn that we must always refer to the interpretations of the scholars instead of merely acting upon the outward meaning of the hadith.
Why did the Prophet not pray for this person although he was a Muslim? It is said that the Prophet did this by way of warning, for perhaps if he had prayed for this person, the people would have thought the man would enter Paradise directly on account of the Prophet’s supplication, and thus they might consider committing suicide themselves. Suicide might then be viewed as a “short cut” to Paradise. It seems the Prophet anticipated such mistaken beliefs, so he did not pray for the man in the hope that other people in the community would not resort to suicide.
Regarding the ruling of the scholars, Abu Eisa At-Tirmidhi reports:
وَاخْتَلَفَ أَهْلُ الْعِلْمِ فِي هَذَا فَقَالَ بَعْضُهُمْ يُصَلَّى عَلَى كُلِّ مَنْ صَلَّى إِلَى الْقِبْلَةِ وَعَلَى قَاتِلِ النَّفْسِ وَهُوَ قَوْلُ الثَّوْرِيِّ وَإِسْحَقَ وَقَالَ أَحْمَدُ لَا يُصَلِّي الْإِمَامُ عَلَى قَاتِلِ النَّفْسِ وَيُصَلِّي عَلَيْهِ غَيْرُ الْإِمَامِ
The scholars disagreed over this issue. Some of them said that the Muslims should pray over every person who prayed toward the direction of prayer (qiblah) including the one who killed himself. This is the opinion of Sufyan At-Thawri and Ishaq. Ahmad said that the leader does not pray over the one who killed himself, but he is prayed over by others.
Therefore, a Muslim who commits suicide should be prayed over by the community, but the Imam may abstain as a warning to others if he believes this will benefit the community.
Success comes from Allah, and Allah knows best.