Selahattin Demirtas, of the pro-Kurdish Peoples’ Democratic Party (HDP), came third in this year’s presidential elections, contesting it from jail.
The ECHR said his detention had the “ulterior purpose of stifling pluralism and limiting freedom of political debate”.
But Turkey’s President Erdogan said the ECHR rulings were not binding.
“We will make a counter-move and finish the job,” he told the state-funded Anadolu news agency.
Turkey is part of the Council of Europe, in which member states agree to follow rulings of the ECHR.
Mr Demirtas, who has been detained since November 2016, faces dozens of charges, and prosecutors are seeking up to 142 years in prison if he is found guilty.
His lawyer said he had applied for release following the ECHR ruling.
Rebecca Harms, a member of the Green Group in the European Parliament and an expert on Turkey, has recently voiced her concerns over the human rights violations in Turkey.
“I am very happy for my friend and colleague Selahattin Demirtaş that the European Court of Human Rights gave this long-awaited and long overdue judgement.
Finally, the highest European court has confirmed that Selahattin Demirtaş’ pre-trial detention is politically motivated and that he must be released immediately. The procedure against Demirtaş as well as the incarceration of so many elected politicians serves President Erdoğan for the weakening and intimidation of the opposition. Therefore this judgement gives also hope to numerous other political prisoners in Turkey.
I call again onto the EU and its Member States to use all their weight, including their economic weight, to press for the release of all unlawfully detained persons in Turkey and to urge President Erdoğan to return to the rule of law.
Vice-President Mogherini and Commissioner Hahn must also ask for the immediate implementation of the verdict, hence the immediate release of Selahattin Demirtaş during their High-Political Dialogue Meeting in Turkey on Thursday.”