By Emre Peker 

ISTANBUL--Turkey's top court on Thursday ruled that a ban on YouTube is unconstitutional, paving the way to lift the two-month blockade, after the government cut off access to Google Inc.'s video-sharing website for publishing leaked state secrets just days before critical March elections.

The Constitutional Court in Ankara sided 14-to-2 with individual appeals filed by Google's local attorney, opposition lawmakers and the Union of Turkish Bar Associations, ruling that YouTube's blockade breached freedom of expression, according to a brief decision published on its website.

Judges next will write up their detailed ruling and send it to Turkey's TIB telecommunications watchdog and the Transport, Maritime Affairs and Communications Ministry for implementation, thus lifting the ban.

Prime Minister Recep Tayyip Erdogan's government so far has refused to implement lower court decisions against the website's blockade. Access to YouTube was still barred late on Thursday.

The Communications Ministry declined to comment, while TIB couldn't be reached after regular business hours.

The YouTube decision is poised to thrust the Constitutional Court back into the center of Turkey's increasingly polarized politics, right before August presidential elections. Mr. Erdogan, who is expected to run for and win the presidency, slammed the judges for removing a similar ban on Twitter Inc. in early April, saying his government will abide by the decision but doesn't respect it.

"The government will see this as a political ruling and attack the Constitutional Court and its chairman," said Soli Ozel, an international-relations lecturer at Kadir Has University in Istanbul. "If the prime minister becomes the next president, he will also get to appoint judges to the Constitutional Court, at which point he can establish court that he desires."

In recent months, the top court has emerged as the last front of a widening political war that has shaken Turkey since nationwide antigovernment protests last June against what demonstrators say is Mr. Erdogan's increasingly autocratic rule.

The premier's AKP Justice and Development Party has dismissed not only the protests, but also a selloff that dragged the lira to record lows and a corruption probe that forced a cabinet shuffle in December, as the work of a what its politicians term a "parallel state" seeking to topple Turkey's government.

Mr. Erdogan says the mastermind of that movement against him is his one-time ally Fethullah Gulen, an influential Turkish imam based in the U.S., with millions of followers at home. Mr. Gulen denies the allegations, but the premier has vowed to go after his congregation, believed to have a heavy presence in the bureaucracy, especially in the police and the judiciary.

"We will make the parallel state account for this, and prevent future coup attempts in Turkey," Mr. Erdogan told AKP lawmakers in the Ankara parliament on Tuesday. The premier has repeatedly alleged that Gulenists have infiltrated the judiciary and even have illegally wiretapped the Constitutional Court.

On Wednesday, Mr. Erdogan claimed that the imam's followers were preparing a film that would be leaked before Turkey's first direct presidential elections in August to harm the premier. An attorney for Mr. Gulen denied the allegation.

The government has responded to challenges over the past year by removing thousands of prosecutors, judges and police officers from their posts, while enacting laws boosting government oversight of the National Intelligence Organization, the judiciary and the Internet.

One of the laws, adopted in February, gave the government power to shutter websites without court order. The legislation was pushed through parliament by AKP deputies after bribery allegations in December rocked the cabinet.

Shortly afterward, the prosecutors' probe became public, recorded telephone conversations--including some of Mr. Erdogan's that he confirmed--were leaked online, widely shared on Twitter and aired on YouTube. The daily revelations came as Mr. Erdogan sought to clinch a victory in March-30 local elections that had turned into a referendum on his decadelong rule.

The government banned microblogging site Twitter on March 20, citing privacy violations and blaming the American company for refusing to abide by local court orders. Twitter says it applies rulings in line with its policies.

YouTube was blocked one week later, just hours after a two-part voice recording surfaced on the website purportedly disclosing a top-secret discussion about a strike inside Turkey's war-torn southern neighbor Syria. The alleged recordings involved high-ranking officials, including Foreign Minister Ahmet Davutoglu and the spy chief, Hakan Fidan. The leaks were decried by Mr. Erdogan as the work of traitors from the parallel state.

Turkey's increasingly harsh online measures have raised concern among its Western allies, with the U.S. and the European Union criticizing recent bans.

But while his detractors charge Mr. Erdogan is becoming one of the world's most determined Internet censors, the premier maintains Web platforms aren't respecting Turkey's sovereignty. The country's interests trump foreign companies' rules, Mr. Erdogan has repeatedly said, slamming social-media websites as menaces that undermine national values.

Yeliz Candemir contributed to this article.

Write to Emre Peker at emre.peker@wsj.com

Subscribe to WSJ: http://online.wsj.com?mod=djnwires