Upon the death of Ibrahim Shah in 1685, his ten-year-old son, Mahmud Shah II, ascended the throne and the state affairs were left to Bendahara Tun Habib Abdul Majid. As he grew up, Mahmud Shah gained a reputation for his caprice, and Johor gradually descended into a state of chaos. This instability was exacerbated in 1697 by the death of Tun Habib. Although Bendahara's son, Tun Abdul Jalil, inherited the position and maintained some stability for a time, Johor was thrown into a state of upheaval in 1699, when Mahmud Shah was assassinated by a local chief, Megat Seri Rama, whose pregnant wife was executed at the Sultan's orders. The assassination was carried out as the Sultan was on his way to a mosque for Friday prayers. The assassination of Mahmud Shah marked the end of the Johor branch of the Melaka royal line and was a profound crisis for two reasons: firstly, the regicide was an act of treason, a grave offence according to the Malay worldview, and secondly, because Mahmud Shah had no known male issue. To fill the vacant throne, the aristocracy proclaimed Tun Abdul Jalil as the succeeding Sultan at Kota Tinggi on September 3, 1699. His accession, however, did not receive the unanimous support of all the various factions within Johor. Many questioned the legitimacy of the Sultan's rise to power. A deeply religious man, Abdul Jalil Shah gradually took less interest in governing his kingdom, devoting more time instead to his faith and to his Acehnese wife Che Nusamah.
In 1715, the Bugis, based in Selangor, sacked Kedah. As overlord of Selangor, Johor claimed part of the spoils. But while Malay custom allocated half the spoils to the overlord, Bugis custom entitled the overlord to only a tenth. Seeking to assert its claim, Johor attacked Bugis strongholds of Selangor and Linggi. Although Johor armies were numerically superior, the Bugis successfully repelled the attack. By 1717, Johor withdrew its forces from Selangor. The refusal of the Bugis to bow to Johor's demands exposed the empire's growing weakness. In addition, opposition factions towards Abdul Jalil's accession resulted several revolts against his regime. The turmoil set the stage for the emergence of the Siak prince, Raja Kecil.
The coming of Raja Kecil
Following Abdul Jalil's contentious accession, Minangkabau settlements in Rembau, Sungei Ujong and Naning that existed since mid 15th century, began to challenge Johor's authority. In 1717, Raja Kecil of Siak appeared on the scene, claiming to be the posthumously born son of the assassinated Mahmud Shah II. Backed by the Minangkabau, he was welcomed by many Johor subjects who hoped for the restoration of the Melaka dynasty. In 1718, Raja Kecil appeared on the Riau River with Minangkabau troops and succeeded in overcoming the Johor fleet. He was then able to capture Johor's capital, then located on Riau Island, and proclaim himself a ruler, adopting the style Sultan Abdul Jalil Rahmat Shah.
Death
Following the victory of Raja Kecil forces, the deposed Abdul Jalil Shah was re-appointed as Bendahara of the kingdom and lived at Kota Tinggi. By the end of 1718, he retired to Terengganu and further removed to Pahang in 1719. On July 11, 1721, he established Kuala Pahang as his new capital and made attempts to recover his kingdom with the support of nobles from Johor, Pahang and Kelantan. There were now three power centres in the Malay world. Raja Kecil in Riau, Abdul Jalil Shah IV on the east coast of the Peninsula and the Bugis in Selangor and Linggi. The Bugis eventually won the day, in part because Raja Kecil had sabotaged his own popularity by having Abdul Jalil Riayat Shah IV killed in Kuala Pahang. Abdul Jalil Riayat Shah IV was killed while conducting his prayers on board his ship, by an emissary sent by Raja Kecil off Kuala Pahang, on November 21, 1721. The Sultan was buried at Royal Cemetery, Kuala Pahang. Raja Kecil finally admitted defeat in 1722, and the Bugis installed Raja Sulaiman, the son of Abdul Jalil Shah IV as the next Sultan of Johor.