The Karabakh khanate was one of 18 state formations, which emerged in Azerbaijan in the mid XVIII c., after long period of Iranian domination. The territory of the khanate was consisted mainly of the lowland and mountainous parts of Karabakh.
The Karabakh khanate emerged and got stronger in the period when struggle for power in Iran and feudal wars in Azerbaijan aggravated. After the Karabakh khanate had been established, the first residence of Panah Ali khan was the Bayat castle, built in 1748 in Kebirly district (mahal). In a short period of time there were built external walls, bazaar, baths and handicraft rows.
Building of the Bayat castle and increasing power of the Karabakh khanate, disturbed, primarily, the local Armenian feudal lords (meliks) and the rulers of the neighboring khanates of Sheki and Shirvan. They agreed to jointly attack Panah Ali khan and besieged the Bayat castle. During a month the allies, headed by Haji Chelebi khan of Sheki, attacked in vain the castle, but couldn’t succeed.
Having been defeated, Haji Chelebi had to retreat with great losses beyond the Kura river and said in dejection: «Till now Panah khan was like a silver without minting. And we came minted him and went back».