I suspect a key part of this is Turkey's NATO membership, as well as its aspirations to join the EU. On one hand, the EU would not take in a military dictatorship and would likely pull back on trade relations. On the other hand, NATO is pickier than it was in the past (when Greece and Turkey had military dictatorships, but there was also the big bad USSR to justify it). Turkey's generals would be getting the cold shoulder from their peers and may, quite possibly, be more interested in being an efficient and honorable military than a political one. Especially as they have no real credible enemy they can point at, besides the perennial saber rattling between them and Greece.
The culture and expectations of Turkish people may also differ from countries where the population is more fatalistic or accepting of dictatorships.
That's Turkey's situation, doesn't apply to many others. Pakistan for one is a very different can of worms (the military originally promoted Islam and they can always point to the "Indian bad guys" to justify their budget and relentlessly jamming their blood funnel into everything).
Erdogan didn't "defeat" the military, he suppressed a coup.
The highest ranking staff officers opposed the coup, and publicly ordered all personnel to return to their barracks.[170] Acting outside the military chain of command, the rebels lacked the coordination and resources to achieve their goals. The conscripted soldiers that the rebels mobilized were uninformed of their mission's true purpose and became demoralized. Many surrendered rather than shoot demonstrators
The military as a whole had already decided to allow democracy to take over. That's how Erdogan got the job 20 years ago and if anything he is the one that is steadily driving Turkey towards ever lower democratic standards.
Wiki is a good place to start with an overview of post-WW2 dictatorships. Not much to generalize about how they end, different ones have ended for different reasons.