Mardavij ibn Ziyar, a native of Tabaristan, and born into an Zoroastrian family, had in around 913 joined the army of Asfar ibn Shiruya, a general in the service of Alids who ruled Tabaristan at that time.
Asfar is a native of Lahijan and a devout Zoroastrian, who claims descent from Shiruyeh (also known as Kavadh II), the patricidal son and successor of Khosrau II.
His name, Asfar, is possibly derived from the Persian term of Asp-var, ("horseman" or "horse rider").
Later, Asfar had taken advantage of a rebellion in the Samanid army and seized power in Gurgan (presently called Golestan) in northern Iran.
Asfar had also taken Amol, Ghazvin, Zanjan, and the city of Rey and appointed Mardavij as the governor of Zanjan.
Due to Asfar's increasingly erratic behavior, a powerful opposition had emerged against him in 927, and the next year Mardavij joins this opposition, defeats Asfar, and takes over Asfar's possessions.
At this time, Mardavij officially founds the Ziyarid dynasty and wears a "crown of Anushirvan" when he holds court.