Erdogan's political career began in 1984, when he was elected chairman of the Welfare Party's office in Beyoglu; in 1985 he was elected head of the Welfare Party's Istanbul branch, and a member of the party's executive board. In 1994, he was elected mayor of Istanbul, and is credited for improving sanitation and the environment in the city, and working to prevent crime. He banned the sale of alcohol in cafes, however, an act which displeased secularists. He served as mayor of Istanbul for over four years.
His devout adherence to Islamic values led to his conviction in 1998 for inciting religious hatred, however. Erdogan had read a poem in 1997 that included the lines, "the mosques are our barracks, the domes our helmets, the minarets our bayonets, and the faithful our soldiers." He served four months in prison but was barred from running for, or holding political office, due to his criminal conviction.
Erdogan became leader of the reformist wing of the Virtue Party after the banning of the Welfare Party in 1998. When the Virtue Party was outlawed in July 2001, he was involved in founding the Justice and Development Party (Adalet ve Kalkinma Partisi or AK) in August of that year and was elected its leader. The AK swept the parliamentary elections of November 2002, winning 363 of 550 seats. Erdogan, still unable to stand for office, remained leader of the AK while Abdullah Gül was named prime minister. In February 2003, Parliament amended the Constitution, allowing Erdogan to be eligible as a candidate in parliamentary by-elections held in March. Erdogan was elected a member of Parliament from Siirt on 9 March. Gül resigned as prime minister on 11 March, and Erdogan became Turkey's new prime minister on 14 March 2003.