The Battle of the Ramganga or Battle of the Rahib in December 1020 AD was a decisive battle, during Mahmud of Ghazni's second invasion of the Doab. Ghaznavid forces defeated the Hindu Shahi army of Trilocanapala at the upper course Ramganga River known as River Ruhut near Afzalgarh. Trilocanapala entrenched on the eastern bank with infantry and elephants to block the crossing. After detecting Trilocanapala's nighttime plan to flee downstream, Mahmud sent eight swimmers across on inflated skins; they repelled Hindu archers and five elephants, secured the bank, and enabled the full army to cross without loss. At dawn the Ghaznavids surprised and routed Trilocanapala's camp after a day-long battle. Trilocanapala escaped wounded; his army suffered heavy casualties, prisoners, and flight. Mahmud captured rich spoils. Trilocanapala was later assassinated by his followers while joining Chandela ruler Vidyadhara
On 27 September 1018, Mahmud of Ghazni, marched along the sub-Himalayan range. He crossed the Yamuna on 2 December and advanced through the Doab. On 20 December 1018, Sultan Mahmud reached Kanauj, seat of the Pratihara king Rajyapala. Rajyapala fled across the Ganges, leaving the city undefended. Mahmud captured its seven forts in a single day, after which Kanauj was plundered. His campaign covered Sirsawa, Bulandshahr, Mahaban, Mathura, Kanauj, Munj, Asi, and Sharwa. On 6 January 1019, Mahmud returned to Ghazni after the success at the first invasion of the Doab.
Following Mahmud's withdrawal to Ghazni in early 1019, the Chandela ruler of Kalinjar Vidyadhara () attacked and killed Rajyapala of Kanauj to punish his flight and surrender to Mahmud's forces. Ibn al-Athir states that Vidyadhara's plans to challenge Mahmud's newly annexed territories provoked Mahmud to launch second invasion in the Doab. Trilochanpal son of Rajyapal was raised to the throne. The Chandelas also assured to help Hindu Shahi ruler Trilocanapala to reclaim his lost kingdom from Mahmud. Therefore, in October 1020 AD, Mahmud marched with his army to crush the Vidyadhara's alliance with Hindu Shahi ruler Trilocanapala and Patihara ruler Trilochanapala of Bari and Kannauj.
Before Mahmud crossed the Ganges, Trilocanapala had already crossed the river with elephants and prepared defences. At Sarbal or Sanbar, he learned that the Shahis were at Ramganga. He swiftly captured the fort of Sarbal, then marched day and night to the Ramganga river. On 15 December 1020, Mahmud overtook the Shahis. There, he found Trilocanapala entrenched on the opposite bank, having crossed near Afzalgarh. The two armies now faced each other across the river, preparing for a direct clash. The upper course of the river Ramganga is called Ruhut. Trilocanapala camped with infantry and elephants on the eastern bank of the river, determined to fight. Mahmud hesitated to cross, as the river was deep and muddy. Trilocanapala initially planned to block the Sultan's army from crossing the river. However, during the night he secretly prepared to flee downstream by boat or raft. When the Sultan learned of this plan he saw how weak and hesitant the enemy had become. He immediately ordered eight of his soldiers to swim across the river using inflated animal skins as flotation devices. Trilocanapala quickly sent a small group of archers along with five war elephants to stop the swimmers from reaching the opposite bank. But the eight Ghaznavid soldiers shot their arrows so fiercely and accurately that the Hindu detachment could not stop them. The eight men landed safely on the other side. Seeing this success the rest of the Sultan's army crossed the river with some riding horses straight through the water and others using inflated skins. All soldiers crossed without any casualties. At dawn the Ghaznavid troops entered Trilocanapala's camp by surprise. They quickly formed ranks and launched a fierce attack on the Hindu forces. Fighting continued for most of the day. Trilocanapala's army fell into complete disorder and was defeated. Trilocanapala himself was wounded in the battle but managed to escape with a handful of followers.
The spoils of victory were immense, with the Sultan's share alone amounting to 270 elephants and two coffers filled with precious stones. Trilocanapala who made attempt to join Vidyadhara was assassinated by some of his own followers. His son Bhimapala who had most likely escaped from Sharwa succeeded his father. Mahmud continued his expedition to Bari. Farrukhi records that among the captives were two wives and two daughters of Trilocanapala. Fakhr-i-Mudabbir further relates an anecdote in which the Sultan, after the capture of Trilocanapala's wife, returned her to her husband with honour, sending her in a howdah adorned with a robe of distinction and golden bracelets.