Afshani was one of the two final candidates shortlisted by the reformist city council, and finally managed to defeat his rival Samiollah Makarem-Hosseini, the acting mayor of Tehran.
Afshani, a member of the reformist party Etemad Melli (National Trust), was elected after securing 19 votes in favour, while only one councillor voted against his election.
The new mayor is currently a deputy minister of interior, and must resign from the post to start his job at the municipality.
During the first term of President Hassan Rouhani, Afshani served as the governor-general of Fars Province.
His election as the mayor of Tehran came after Mohammad-Ali Najafi, another reformist politician, resigned earlier this year over what he called his health problems.