Under the peculiar Turkish constitution, the army is actually charged with preserving the constitution and in particular the secular nature of the state. IIRC, the government is obliged to cede power to the Military Council when asked to do so by the military high command. If they do not do this, the army steps in and makes them. When these steps are followed, it is a legal and constitutional process... however what happened last week was an intervention following a coup within the military; the intervention was therefore not constitutional. However one could argue that the army still had a duty to step in and preserve the democratic and secular nature of the state, especially since Erdogan had already purged the military leadership and replaced them with his cronies, bypassing this constitutional safety valve.
With that said, there is an increasing amount of indication that this coup was staged. The small scale of the whole affair, the strange decisions made by the military insurgents (they went for loudness rather than effectiveness), the ease with which groups of them surrendered (according to some rumours, a lot of the soldiers were just conscripts thinking they were going on a military exercise), the repeatedly reported lack of any attempt to go after or at least capture high ranking government officials, followed by the sudden emergence of stories of miraculously narrow escapes by some of them, including the Heroic Leader. And of course the incredible far-reaching purges that were set in motion moments after the coup was suppressed. There's no proof this was staged, and even if it was I doubt we'll find evidence in the leaked emails, but I still say something smells. Bad. If you want to stage a coup without doing too much damage and without the danger of it escalating into an actual coup, then this is how to do it.