The decision by the Pennsylvania Supreme Court came after Cosby had served more than two years of a potential three- to 10-year sentence in a state prison following his 2018 conviction.
The 83-year-old comedian and actor was best known for his role as the lovable husband and father in the 1980s television comedy series The Cosby Show, earning him the nickname "America's Dad."
But his family-friendly reputation was shattered after dozens of women accused him of sexual assault over a period of decades.
His conviction was widely seen as a landmark moment in the #MeToo movement that brought forth an array of allegations against powerful men in Hollywood and beyond.
He could be released as soon as later on Wednesday from a state correctional facility in Shippack, Pennsylvania.
Cosby was found guilty of sexually assaulting Andrea Constand, a Temple University employee, in his home in 2004 after drugging her with unidentified pills.
The court's majority found that a state prosecutor, Bruce Castor, struck a deal with Cosby's attorneys in 2005 not to bring criminal charges.
As a result, Cosby was unable to avoid testifying as part of a civil lawsuit that Constand brought against him, since defendants can only refuse to testify when faced with criminal prosecution.
His sworn deposition, which a judge unsealed in 2015, eventually led a new district attorney, Kevin Steele, to charge Cosby later that year, just days before the statute of limitations was set to expire.
That prosecution, the court found, essentially amounted to reneging on Castor's earlier promise not to charge Cosby.
"In light of these circumstances, the subsequent decision by successor D.A.s to prosecute Cosby violated Cosby's due process rights," Justice David Wecht wrote for the majority.
"There is only one remedy that can completely restore Cosby to the status quo ante. He must be discharged, and any future prosecution on these particular charges must be barred."