After the overthrow of the Afshar Empire led by Nadir Shah the prominent state official Panahali bey Djavanshir founded one of independent states of Azerbaijan - Karabakh.
He for some time worked under Nadir shah. Yet after the Mughan congress Nadir laid punishment on those Karabakhians who did not recognize his power and exiled Muslim population to Afghanistan and Khorasan.
Panahali's brother Farzali khan, who protested against these measures was put to death. In period of the Shakh staying in Khorasan, Panahali bey took the occasion and run away to Karabakh province together with his relatives and closes in 1737-1738. Shah informed about his escape sent couriers after him to prevent him from running away. Nadir shah instructed the leaders of Azerbaijan, Ganja, Tiflis and Shirvan to catch Panah khan wherever they see him and send to the shah's residence. Though by the order of the shah Panahali and his family were tormented and set a penalty, that did not work.
Thus, Panah ali khan dodged the responsibility to subdue to Nadir shah and bid to rule his native Karabakh in the most democratic way. As a result, an independent Azeri state-Karabakh khanate established following the death of Nadir shah.
After the declaration of the establishment of independent Karabakh khanate the main goal was to strengthen it. The first measure taken by Panakhali was to return those Muslims driven by Nadir shah to their native land. The return of displaced families and their resettlement of their native land strengthened Karabakh khanate. The future khan of Karabakh 15 year-old Ibrahimkhalil was among those who had been first to return.
In the period of Karabakh khanate establishment, it did not comprise cities as Tebriz, Ardebil, Ganja, Shamakhy, Baky, Nakhichevan, Sheki, Derbend. However, the existence of such politico-economic centers would play a great role in the further development of the khanate. Beside military-political works Panah Ali also made great progress in the military installations and establishment of towns.
One of the first steps taken in this sphere was the construction of the Bayaty tower named after the ancient Azeri-Turkish tribes bayats in 1748.
Panah khan's activity in the sphere of creation of an independent state promoted his official recognition. An unsuccessful Bayat raid of Sheki khan Hajy Chelebi in 1748 to Karabakh played a great role in the recognition of Panah Alia as a leader of Karabakh khanate. At the same time the Bayaty battle proved the vulnerability of the tower to further historical events. Therefore, it was necessary to construct a new one. In 1751 the construction of tower (Shusha) was completed and khan settled there.
With the strengthening of Panah khan the feudal lords-monarchs, supporting scarcity, increased their subversive activity. It was necessary to prevent that by some military methods. Otherwise, the khanate would lose its territorial integrity. The original sources also confirm that.
Thus, historical facts show that except for Khachyn other four melikhood of Karabakh and their ancestors were not the native Karabakh residents and they had moved to the region from other places. They were not Armenians, they represented former Albanian generations. Therefore, it's incorrect to justify the territorial claims of Armenian nationalists and to regard the said malikhoods (rulers) as the followers of the Armenian state structure. On the other hand, alien rulers after the capture of power in Karabakh did not manage to unite the country. They lived isolated and could not overcome the limits of leaders feuding with each other.
Melik Shahnazar was the first among the five feudal lands to recognize Panah Ali's power. That was due to the marriage of Ibrahimkhalil to Melik Shahnazar's daughter Hurzat.
After the defeat of Khachyn melik at Ulubab Ballygaya, he recognized the power of Panah khan, while the hostile policy of Dizag, Chilebord and Talish lands lasted for several years.
The path of Panakh Ali's progress in overriding the feudal lands was followed by his son Ibrahim khan.
Among the Karabakh feudal lands, Melik Yesay of Dizaq, Medjnun of Chilebord and Beyleryan of Gulistan refused to subdue to Ibrahim, while Melik Shahnazar of Varanda and Mirza khan of Khachyn recognized his power and joined the patriotic troops created by Ibrahim.
The allies encircled the Tug tower in 1781. Melik Yesay surrendered and the power transferred to Melik Bahtam. Soon he turned out to be a betrayer as well.
Beginning from 1783 Russia also started to interfere with Ibrahim khan's struggle against separatist meliks. By the help of local meliks Russia, willing to occupy South Caucasus attempted to establish a Christian state or the support for itself in Azerbaijan. At that time owing to his high diplomatic capacities Panahali managed to assemble all hostile meliks in Shusha. Proving their betrayal by some documents he imprisoned all of them.
Melik Medjnun was imprisoned while Melik Bahtan due to his misdemeanors was delivered to the khan of Ardebil..
Agha Mohamed Gajar (1742-1797), who seized power in Iran in the summer of 1795, attacked Karabakh khanate. The 33-day encirclement of Shusha was failed. After Shusha, he moved on to Tbilisi.. Agha Mohamed Qajar again attacked Karabakh, occupied Shusha but self was killed there in 1797.
The invasive activity of Russia strengthened in the South Caucasus, including Azerbaijan in late 18th-early 19th century. In 1801 Georgia was annexed to the empire, Azerbaijan's Djar-Belokan (1803) community and Ganja khanate (1804) were occupied.
Under such conditions, Ibrahim khan concluded an agreement with the commander of Russian troops P.D.Sisianov (1802-1806) in Kurekchay. The agreement stipulated for Karabakh khanate to be annexed to Russia as a Muslim country-the Azerbaijani land. The Kurekchay agreement, which reflected the reality, is the most reliable document which proved that Karabakh, including its mountainous part, belongs to Azerbaijan.