The Qajar dynasty originated as a local
Turkoman noble family in northern Iran, under the Safavids. The Qajars gradually increased in power as other families fought each other in Iran, culminating in
Agha Mohammad Shah proclaiming himself ruler in 1789, in opposition to the Afsharids and Zands. Agha Mohammad defeated the Zand dynasty in 1794 and was officially crowned in 1796. Shortly thereafter, he captured and deposed the Afsharid Shahrokh Shah, reunifying Iran under a single ruler.