By Syed Shoaib Hasan
BBC News, Islamabad
Mr Musharraf argues his actions were necessary to combat militancy
Pakistan's Supreme Court has ruled that emergency rule enforced by former President Pervez Musharraf was unconstitutional and illegal.
The order clears the way for him to be tried for treason because under the constitution, anyone found guilty of abrogating it can be prosecuted.
The court ruling also said that the appointment of judges after the emergency was illegal.
However, the order will not affect the position of President Asif Ali Zardari.
It said that questions over the constitutional legitimacy of his appointment should be "exempted".
The court ruled that if this matter was brought before it, there was a danger Pakistan could be plunged into another constitutional crisis.
But the ruling does mean that those judges who were appointed after the emergency was imposed could lose their jobs.
Significantly though, the ruling did not touch on the National Reconciliation Ordinance, which the former president issued just before he enforced emergency rule.
The former president still has some support in Pakistan
The ordinance gave an amnesty from criminal prosecution to several leading politicians, many of whom went on to win the 2008 elections.
Most prominent among these is President Zardari.
The ruling had been delayed for several hours as the judges debated the exact wording of the order in their chambers.
Outside, in the courtroom and around the court premises, hundreds of people including lawyers, politicians and journalists waited impatiently for the decision.
When the ruling was finally declared, ecstatic lawyers and political workers distributed sweets and danced to drum beats in celebration.
The 3 November 2007 emergency rule enforced by President Musharraf was highly unpopular and also marked the beginning of the end of his rule.
He eventually had to pull back the emergency and hold general elections which led to the defeat of his political allies in the following February.
President Musharraf was finally forced to resign in August 2008 after unrelenting political pressure from an opposition movement.
This was led by his nemesis, former Prime Minister Nawaz Sharif, who General Musharraf overthrew in a 1999 military coup.
The ruling marks the removal of the final pieces of an extra-constitutional package President Musharraf had used to consolidate his grip on power.
On Thursday the court rejected a request to launch a treason case against the former president.
It said that parliament was the best place to debate Mr Musharraf's actions.