Twenty-two Turks charged with terrorism offences after being arrested at an anti-government protest have been freed on bail after six months in jail in a case that rights activists say highlights the government's growing intolerance toward dissent.

Prime Minister Tayyip Erdogan and his pious, conservative AK party this year swung round from friendly ties with autocratic Arab leaders to strong support of popular Arab Spring protests and have held up Turkey as an example of advanced democracy.

But at home, right groups and opposition parties accuse the government, in power since 2002, of a gradual monopolisation of state institutions, including the police and judiciary, and using sweeping anti-terror and defamation laws to jail critics.

Police arrested 28 people in June at an Ankara protest against the death of a retired teacher who suffered a heart attack when police fired pepper spray at a demonstration against Erdogan in the Black Sea town of Hopa a few days before.

Police raided their houses and found left-wing tracts and pictures of executed Turkish leftists. While no weapons were found, police seized a broken umbrella as evidence, media said.

Prosecutors charged 22 of them, including 14 students, with crimes ranging from membership of a terrorist organization, to committing crimes in the name of a terrorist organization, crimes that could carry a sentence of up to 45 years in jail.

Hundreds of protesters gathered outside the Ankara court on Friday for the first hearing of their case after the accused had spent nearly six months in jail, shouting slogans including we will not surrender to the AK Party, and the youth will bring the AK Party to account.

But after a 12-hour hearing the judge ordered the 22 freed on bail and demanded the files of an investigation by prosecutors into whether the accused had been tortured in custody. Six others, accused of lesser charges, were already out on bail.

Government officials deny any attempt to muzzle opposition and say the detention of journalists, students, military officers and others for months without trial is the result of a cumbersome, inefficient judiciary badly in need of reform.

(Writing by Jon Hemming)