Battle of Gegodog

The Battle of Gegodog (also spelled Battle of Gogodog) took place in 13 October 1676 during the Trunajaya rebellion, and resulted in the victory of the rebel forces over the Mataram army led by the Crown Prince Pangeran Adipati Anom. Gegodog is located in the northeastern coast of Java, east of Tuban.
Prior to the battle, Trunajaya invaded eastern Java and occupied Surabaya and other towns. King Amangkurat I sent an army to face him, led by the crown prince. The crown prince expected a sham battle from Trunajaya, his former protégé. However, Trunajaya offered a real fight which resulted in a decisive victory over the much larger royal army. The royal army was routed, and the king's elderly uncle Pangeran Purbaya was killed after leading a futile charge. The rebel victory was followed by further successes - including more conquests and the defections of Mataram subjects into Trunajaya's side.
Background.
Prior to the rebellion, the Madurese nobleman Trunajaya lived in exile in Mataram and had a close relation with the crown prince of Mataram (then known as Pangeran Adipati Anom, future Amangkurat II). Trunajaya's father-in-law and a prominent nobleman Raden Kajoran had introduced him to the crown prince in 1670. Both had a grudge against King Amangkurat I, the crown prince over the execution of Pangeran Pekik, the prince's maternal grandfather, and Trunajaya over his exile and the execution of his father. The two forged a friendship, partly due to this mutual dislike. In 1670 or 1671, Trunajaya left Mataram for his homeland Madura and became its ruler. He used the crown prince's patronage (as well as his own family's name) to gain followers which allowed him to wrest control of the island.
The Trunajaya rebellion began in 1674 as Trunajaya's forces conducted raids against the cities under Mataram control. In 1676, a rebel army of 9,000 invaded East Java from their base in Madura, and took Surabaya – the principal city of East Java – shortly after. The army consisted of East Javanese, Madurese and Makassarese and was led by Trunajaya and his Makassarese ally Karaeng Galesong.
Battle.
In 1676, the King decided to send a large army to suppress the rebellion. The army was much larger than Trunajaya's, but was predominantly made up of conscripted peasants. It also included West Javanese auxiliaries. The king put the crown prince in charge of this army; he was either unaware of the crown prince's role in the rebellion or planned to have him murdered during the campaign. Other princes also joined the army, including the King's uncle Pangeran Purbaya, Sultan Agung's only remaining brother who was almost 80 years old, as well another son of the king Pangeran Singasari who was the crown prince's archenemy.
The Mataram army marched to Jepara, and then eastwards towards territories controlled by the rebels. It met the rebel army in Gegodog, east of Tuban on the north coast of eastern Java. The Crown Prince initially expected that he and his protégé Trunajaya would only fight a sham battle and then both forces would join against the King. However, Trunajaya betrayed his former friend and patron, and the presence of other princes might have prevented the crown prince from faking the battle. After a long wavering, the prince ordered an attack on 13 October. Trunajaya offered a real battle, which went badly for the royal army. In a desperate effort, the aged Pangeran Purbaya rallied the troops and led a final charge. According to Thomas Stamford Raffles, he "performed extraordinary feats of valour", had his horse shot under him and continued to fight on foot, before he was overpowered and slain. The charge failed, and the battle ended in a decisive victory for the rebels. Mataram troops disintegrated and retreated, along with the Crown Prince and other princes, towards the capital. 
Aftermath.
After the rebel victory, Javanese defection into Trunajaya's side accelerated, and Trunajaya followed up his victory by further conquests of Mataram's territories. His forces advanced westward along the northern coast, and by January 1677 nearly all of Mataram's northern coast, with the notable exception of Jepara, fell into his hands. Towns as far west as Cirebon submitted to Trunajaya. Jepara did not fall due to the united efforts of the king's military governor there and VOC forces who arrived there from Batavia. The inland advance was slower, but rebel forces under Raden Kajoran would ultimately overrun and sack the capital in June 1677.
For Mataram, the defeat was disastrous. After the battle, Mataram could only wage a defensive campaign. Its territories, expanded by Sultan Agung decades before, would fall into rebel hands, laid in ruin and their fortifications dismantled, culminating in the fall of the capital. The rebellion would continue for several more years, and Mataram would be forced to request assistance from the Dutch East India Company (VOC) - in exchange for geopolitical and financial compensations - to turn the tide of the war. 
The crown prince was blamed for the defeat and was accused of colluding with the enemy. After Gedogog, he was replaced by his brothers, Pangeran Martasana and Pangeran Puger, in leading the Mataram forces. After the fall of the capital he fled with his father, and upon his father's death during the retreat, he took the throne as Amangkurat II.