* Top court rules defendants' rights were violated
* Ruling may foster thaw in military-government ties
* Conflict in Syria and Iraq presents challenge to military (Adds officers leaving jail and comments)
By Daren Butler
ISTANBUL, June 19 (Reuters) - Hundreds of military officers convicted of plotting to topple Prime Minister Tayyip Erdogan began emerging from jail on Thursday, a day after the country's top court ruled their trial was flawed.
There were emotional scenes outside prisons in Ankara and Istanbul as the released soldiers were reunited with their families who greeted them with Turkish flags and flowers, some shedding tears of joy.
The Turkish military said it "shared the joy of the families of retired and active staff who have regained their freedom" and said it hoped a retrial would now lead to a just verdict.
The 2010-2012 "Sledgehammer" trial marked a high point in Erdogan's drive to tame an army that for decades had dominated politics. Critics accused Erdogan at the time of using the courts to pursue a "witchhunt" against the generals.
In consigning hundreds of senior serving as well as retired officers to jail, the case eroded the authority and power of NATO's second biggest army while tension on the borders with Syria and Iraq demanded increased commitments.
The constitutional court ruled unanimously on Wednesday that the officers' rights had been violated in the handling of digital evidence and the refusal to hear testimony from two former top military commanders, as requested by defendants.
"This case will only be closed when those who have plotted it are in jail," former First Army commander General Cetin Dogan, a chief suspect in the case, told reporters outside Istanbul's Silivri prison after his release.
"So that they don't set up innocent people again."
"Obviously I'am happy my husband is being released but it is a bitter-sweet happiness," his wife Nilgul Dogan told Reuters. "These people were held unjustly in jail for four years due to a fabricated, wrongful, unjust trial."
"They must give account to us for those four years. After our husbands have rested for some time, we will continue to fight for this country for our children and grandchildren."
Erdogan, his primacy over the army established, said early this year he was open to the idea of a retrial. Officials had suggested evidence had been manipulated by a Islamic cleric who had been using his influence in the police and judiciary to help Erdogan break the army's power.
Cleric Fethullah Gulen, a former ally of Erdogan turned bitter rival, denies any involvement in Sledgehammer and in a corruption investigation against Erdogan associates that the prime minister accuses him of concocting to topple him.
Release of the officers was not generally expected to undermine Erdogan's popularity and could even, at this stage, help ease relations with an officer corps he has excluded from policy-making bodies since coming to power in 2003.
The generals, who removed four governments in four decades, viewed Erdogan with suspicion because of his Islamist past. But his popularity - he has won three elections in a row - afforded him protection while he moved to break their power.
"The constitutional court has restored the Turkish judiciary's reputation which has been in a shambles because of the lawless acts that special authority courts carried out," said Engin Alan, a former lieutenant general and lawmaker for the nationalist opposition party MHP told supporters outside Ankara Sincan prison.
"Our struggle will continue until these dishonourable people who have planned, carried out and supported this malignity will stand in front of the just and honourable judges of this country and be given the punishment they deserve."
ERDOGAN FOCUSED ON ELECTION
In March, a court ordered the release of a former military chief and other defendants accused of the separate "Ergenekon" plot to topple the government.
Erdogan, who is expected to seek the presidency in an August election, is now focused on battling U.S.-based cleric Gulen, whom he also accuses of trying to unseat him.
The election comes at a time of heightened tensions on Turkey's frontiers. The armed forces have deployed additional defences on the Syrian border to cope with spillover from civil war there and a Sunni insurgency in Iraq has also raised alarm.
More than 300 officers were sentenced in September 2012 over the alleged "Sledgehammer" conspiracy and the appeals court upheld their convictions last October.
The alleged plot dates back to 2003, months after Erdogan first came to power, and was said to include plans to bomb mosques and trigger a conflict with Greece by shooting down one of Turkey's own warplanes to trigger a military takeover.
Turkey's armed forces were long the guardians of the secular republic established by Mustafa Kemal Ataturk, carrying out three coups between 1960 and 1980 and pushing an Islamist-led government from power in 1997.
Since first coming to power, Erdogan's Islamist-rooted AK Party has reined in army influence with a series of reforms designed to strengthen democracy, while prosecutors have pursued suspected coup-plotters in the army through the courts.
The leading defendants in the Sledgehammer case were Dogan, former air force commander Ibrahim Firtina and retired admiral Ozden Ornek, who were given 20-year prison sentences.
Sledgehammer and other trials sparked accusations that the government was using courts to silence political opponents.
Former army chief Kenan Evren, 96, was sentenced to life in jail on Wednesday for leading a 1980 coup that resulted in widespread torture, arrests and deaths. (Writing by Daren Butler and Humeyra Pamuk; Editing by Nick Tattersall and Andrew Roche)
Our Standards: The Thomson Reuters Trust Principles.