Fethollah ordered Erdoğan to say stupid things to show him as authoritarian stupid dictator to west, between 2015-2016. The fight began before the coup actually, i just didn't want to go deep since i just wanted to give you a tip what happened, as a summary. Erdoğan was not meant to stay there, it was a tool to be used and thrown away in order to destabilize and divide Turkey. When Fethollah couldn't arrest Erdoğan and his family back in 2015, Erdoğan started to fight them back by cleansing institutions such as police/MIT/judgery and other critical institutions from Fethollah militans.
Fethollah tried to make a coup before it's so late, there was going to be YAŞ (High Military Meeting) decisions where Erdoğan was going to purge last Fethollah guys in Army in August 2016, so Fethollah used their last chance to take Erdoğan down and start a civil war in Turkey, by trying a military coup, one month before the YAŞ, in 15 July 2016.
Erdoğan should have been kicked out or killed for Fethollah's benefit, because he was a puppet, and Fethollah's himself was meant to be replace Erdoğan and Fethollah would become the Houmeyni of Turkey.