The court ordered Mr Khan's immediate release. His lawyers had argued that his detention from court premises in Islamabad on Tuesday was unlawful.
At least 10 people have been killed and 2,000 arrested as violent protests have swept the country since he was held.
BBC reports Tuesday's arrest escalated growing tensions between him and the military.
The opposition leader, ousted in a confidence vote in April last year, was brought to court on the orders of Pakistan's top judge.