Despite Turkey's political divisions, Istanbul's mayor and other AK party leaders joined the opposition demonstrators.
Posters at the rally proclaimed "No to coups".
In his speech, CHP leader Kemal Kilicdaroglu said that amid the turmoil, "the parliament stood proudly, Turkey stood proudly, MPs stood proudly, people in this square have stood proudly, and democracy won!"
But he also stressed the importance of a free press and freedom of assembly, as well as the dangers of dictatorship and authoritarianism.
He said: "The state cannot be governed by grudge, anger and prejudice. Those responsible for the coup should be tried lawfully, with the understanding of abiding by the rule of law."
In a rare move, pro-government television channels broadcast the speech live.