STAMFORD, Conn. — After spending more than a decade behind bars for the murder of a teenage girl in Greenwich, Conn., Michael C. Skakel, a cousin of the Kennedys, was ordered free from prison on Thursday to await a possible retrial.

Judge Gary White of Connecticut Superior Court in Stamford set bail at $1.2 million, ordering Mr. Skakel not to leave the state without permission and to wear a tracking device. The bail hearing came after another judge ruled last month that Mr. Skakel did not receive a fair trial because his first lawyer, Mickey Sherman, had not represented him effectively, depriving him of his constitutionally guaranteed right to counsel.

As Judge White made his announcement, friends and relatives of Mr. Skakel burst out in applause. Mr. Skakel, dressed in a suit and blue tie, tapped his chest as he walked out of the courtroom. Shortly after 2 p.m., he emerged from the basement of the courthouse and embraced supporters in enthusiastic bear hugs. He stood silently while his lawyer spoke to the throng of reporters outside before being whisked away in a waiting car.

Hubert J. Santos, Mr. Skakel’s current lawyer, declined to publicly reveal where Mr. Skakel would go once he was released. His brother John Skakel, who lives in Portland, Ore., provided bank checks to cover the bail.