Mr Khan left the court grounds, heading for his hometown of Lahore, amid tight security. The arrest, which Pakistan's Supreme Court ruled as "invalid and illegal" a day earlier, has further fueled unrest in the country of 220 million people. Pakistan is in economic crisis, with record inflation, weak growth and the International Monetary Fund (IMF) delaying bailout.
Mr. Khan welcomed the court's order and said that the judiciary is the sole protector of Pakistani law. "I have to say that I expect this from our judiciary, because that's the only hope right now..." he told journalists inside the courthouse.
Many cities in Pakistan have witnessed violent protests after Mr Khan was arrested by anti-corruption authorities on Tuesday. Mr Khan denies any wrongdoing.
His supporters stormed military facilities, set fire to the state TV station building, smashed buses, ransacked the home of a top military official and attacked other properties, leading to nearly 2.000 arrests and troops deployed.
At least eight people have been killed in violence that has exacerbated the country's unrest and dashed hopes of resuming a key International Monetary Fund bailout.
Pakistan's military has warned against further attacks on its properties, calling the violence "premeditated". The Pakistani military still plays important political and economic roles in the country. However, the Pakistani military said it supported the democratic process.
Mr Khan, 70, a cricket hero turned politician, was ousted from his position as prime minister in April 4 in a vote of no confidence by the Pakistani Parliament and remains the elected leader. country's favorite according to opinion polls.
Huy Hoang (According to Reuters, AFP)