A teenager was released from custody on Friday after his arrest for allegedly insulting Turkish President Recep Tayyip Erdogan caused uproar in the country.

The 16-year old student, identified by his initials M.E.A, was taken away from his school on Wednesday and jailed for making a speech during a student protest in which he reportedly said Erdogan was regarded as the "thieving owner of the illegal palace." It was a reference to a government corruption scandal as well as a controversial 1,150-room palace Erdogan inaugurated in October.

Turkish Prime Minister Ahmet Davutoglu had defended his detention, saying the presidential office had to be respected, but opposition parties denounced his arrest as the latest example of the government's crackdown on dissent in the country.

A court in the central Turkish city of Konya agreed to free the boy from police custody on Friday, after dozens of lawyers volunteered to defend him and petitioned for his release.

It is a crime in Turkey to insult the president and the boy still faces up to four years in prison if charged and convicted.

Dogan news agency footage of his release showed the boy being embraced by his mother as he walked through the gates of a detention center. Dozens of his supporters sang and beat a drum in celebration.