ANKARA—When Recep Tayyip Erdogan rose to power in 2003, he neutralized a potential threat from Turkey’s secular military leadership by promoting like-minded Islamists who were followers of a key ally, the imam Fethullah Gulen.