The Conquest of Kabul (1504)
As we know it today, Afghanistan did not exist in the 14th century as a unified country with fixed borders. It was more of a region than a country. The region was divided into various smaller kingdoms, tribal territories, and regions that were part of larger empires. Afghanistan's history was marked by constant political changes, invasions, and power shifts during this time. The Maurya empire controlled part of Afghanistan, particularly under the rule of Emperor Ashoka in the 3rd century BCE.
Ulugh Beg II was the paternal uncle of Babur, and he had inherited the cities of Kabul and Ghazni from his father. Ulugh ruled Kabul for four decades (https://en.wikipedia.org/wiki/Ulugh_Beg_II). His son was too young to take the throne when he died in 1502, just before the Battle of Archiyan. There was a war of succession after the death of Ulug, and Babur used the unrest to his advantage and seized Kabul with very little opposition.
He left a sizeable estate to his cousin and, in return, became the king of Kabul in October 1504. He must have breathed a sigh of relief as he finally had a home. Having secured a base in Kabul, he proceeded to subjugate the rest of the Afghan tribes and conquered the tribes in several areas, including areas close to the Sindhu River, and returned to Kabul in May 1505.
The Demise of Shaibani Khan and the Aftermath (1510)
Shaibani Khan's power and influence were growing outside Uzbekistan, and he conquered parts of Afghanistan in June 1507. Shaibani Khan's influence had now extended to Afghanistan, which must have alarmed Babur because It wouldn't take much time for Shaibani to attack Kabul. If Kabul fell, Babur would be homeless again. Babur had very little chance against ShaibanKhan’s professional soldiers and equipment.
Babur's Shaibani Khan worries ended in 1510 when Shaibani Khan ended up in a conflict with Shah Ismail, the Shah of Iran and the head of the Safavid dynasty. Shaibani Khan was killed at the Battle of Merv in 1510.
Babur's Relationship with Persia (1511 – 1514)
The death of his arch-Uzbek rivals rekindled hopes of restoring the lost territories in Babur. Babur was already maturing into an astute diplomat, and he realized the importance of forging relationships with the mighty ruler of Persia. Via sending his emissary, Babur established his relationship with Persia in 1511. With Shaibani Khan's demise and Persians' support, Babur captured his ancestral Farghana, soon marched to the famed Samarkand, and captured it again with broad popular support. The Shah wanted Babur to show concessions to the Shiites in Samarkand in return for providing support, which Babur did.
This upset the predominantly Sunni population, and they revolted against Babur, and he lost Samarkand again for the third time. The Shah ended up in a conflict with the Ottoman Turks and was killed in Tabriz in August 1514. The Uzbeks were rising again, and with the Persian support gone, Babur had no chance of having a Timurid–Safavid alliance to counter the Uzbeks. He had little interest or hope in Central Asia then and returned to Kabul.