At the end of 11th and at the beginning of 12th centuries, the empire of Seldjukids entered to the period of collapse at the result of interior fought for throne among the representatives of Seldjukid dynasty. Utilizing from this circumstance, several governors, who were under the subordination of seldjukid state, refused to accept the authority of Sultans. SirvanShahs, which had to be under the vassality of different, more powerful and great neighborhood states was among these governors. At this time, “the tsar of georgians and abkhazians”, David IV (1089-1125), who married one of his daughters with Byzantine prince, the other with the son of ShirvanShah and the heir of the throne, did his best completely to liberate from seldjukid supremacy and strengthen the security of his realm. At the expense of marriage diplomacy the tsar wanted to enlarge the borders of the Georgian realm; so with this aim he married to the daughter of Kepchak khan and settled 40 thousand kepchak families in Georgia (1118), in order to utilize from them in his political goals. For instance, tsar David IV intervened to the territory of his relative, ShirvanShah Afridun I in the years of 1117-1120. His army, which mostly consisted of khipchaks, plundered Arranian cities of Khunan, Barda, the regions of Shirvan, destroyed the cities of Shaki, Gabala and Qorchevan. During 1121- 1123, David IV managed to include the Muslim possessions of Tiflis and Tumanis (Dmanisi) at the result of the victory against the governors of Seldjukid Sultan, Toghrul and Mahmud who fought for the throne.
After the death of Afridun I, who was murdered in the battle for Derbent, the throne in Shirvan passed to his son, Manuchohr III (1120- 1160). Manuchohr III was under the influence of his wife, Georgian princess Tamara and maintained pro-georgian orientation, i.e. the side of his father-in-law, David IV in the policy. He completely rejected to pay taxes to Seldjukid Treasury. Depriving from the taxes in the amount of 40 thousand dinars, the Seldjukid Sultan Mahmud directed to Shirvan at the beginning of 1123, captured Shamakha and captivated ShirvanShah.
The excute for this attack was written appeal of merchants, craftsmen and Shirvan feudals, which disagreed with the destructive campaign of David IV and the behavour of Manuchohr III.
David IV learnt about it and directed to Shirvan for the aid to his ally and son-in-law, Manuchohr III with 30 thousand troops. But, there happened a conflict between Georgians and khipchaks, at the result the Georgians escaped. After that Sultan Mahmud also did not enter to the battle, left Shirvan and returned to Isfahan. It was essential that Seldjukids did not appear in Shirvan for decades after this event and that is why, we could consider that there began political prosperity in Shirvan till Mongolion conquests. But, anyway, Georgian tsar again attacked to Shirvan in June, 1123, captured the cities and fortresses of Shamakha, Bughurd, Gulustan, Shabran for short time.
After the death of David IV in 1125, his son Dmetri I came to the throne and during his authority that had lasted for 30 years (1125-1156) the Shirvan-georgian relations stabilized, even were characterized as close allied relations. The 33-35 years of ruling of Manuchohr, who returned from Isfahan were the years of prosperity and development for Shirvan. The region, which rescued from foreign invasions revived, destroyed cities and villages by Georgian-khipchak troops were restored; there were built fortress walls around Baku, as well as there was constructed new bridge over the river of Kur, Manuchohr even had to directed to Georgia in order to help his ally – Shirvanian troops pressed the military uprising of hired kiphchaks against Georgian tsar.
ShirvanShah returned to Arran with victory.
According to some sources, after the death of Manuchohr III, his widow Tamara attempted to join Shirvan to Georgia, but in the local fought of his brothers for the throne prince Ahsitan won and obliged Tamara to return to Georgia (where she died after 2 years in one of churchs).
Ahsitan conducted independent policy, created close relations with Georgian tsar, as well as with the state of Azerbaijan Atabeys in the presence of Shamsaddin Eldeniz and Jahan Pahlavan. When atabey Gizil Arslan came to the throne in this state Ahsitan attempted to intervene the interior affairs of the state of Eldenizes, but he was defeated. In the response to this, Gizil Arslan invaded Shirvan in 1191, reached to Derbend and subordinated the whole Shirvan to his authority.
In 1192, the earthquake dectructed the capital of Shirvan, Shamakha; there died more than thousand people, also the family members of Ahsitan. That is why he moved the capital of state to Baku.
In 1191, Ahsitan participated in unsuccessful battle against atabey Abu-Bekr (the battle was near Beylagan) together with pretender to the throne of Atabeys, Amir Amiran. But, in 1195, the allies managed to defeat Abu-Bekr and occupied Arran. But, in 1197, Amir Amiran died in Gandja and his father-in-law Ahsitan died in Shirvan. So, the active participation of Shirvan in general events of Azerbaijan was over. The 37 years of ruling of Ahsitan I is considered the period of prosperity of Shirvan in the presence of Kesranids dynasty.
As Ahsitan did not have a successor from male sex because of the earthquake of 1192, his brothers and nephews had ruled the state for 8 years, which caused to the deepening of political conflicts and interior foughts. Then, during the reign of ShirvanShah Fariburz II, Farrukhzad I and Gershasp I the state lived through its worst days. At the end of Gershasp’s I authority Shirvan was sprained to the invasion of Mongolian troops, led by Cebe and Subutay (first compaign of Mongols), also Kepchaks and Georgians. Finally, in the presence of Faribur III, although Shirvan was sprained to the second compaign of Mongols (1231-1232 s), was subordinated to Mongols and lost its independence, the authority of the dynasty of Kesranids stabilized and the relations with Georgia was broken off. So the 12th century, especially the second half of this century became one of the phases in the history of ShirvanShahs, when this state lived through the period of strengthening.
In this period the borders of the state stretched to Derbend in the North, to the river of Kena in the South (sometimes including Beylagan and Mughan), to the region of Shaki and the possessions of the state of Azerbaijan Atabeys in the West.