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.
As Mr Khan arrived in court, media ran through the corridors to capture his first public appearance since he was arrested.
Surrounded by security, Mr Khan said nothing as he walked to the wood-panelled courtroom which was filled with officials from his party and journalists.
Mr Khan stood surrounded by his lawyers in front of the three Supreme Court judges as they told him that because of the way he had been arrested on Tuesday - inside a court complex, conducting biometric tests - the arrest was invalid.
Footage of his arrest showed paramilitary forces seizing Mr Khan, who was injured in a gun attack last year, and dragging him from inside court premises, before whisking him away in an armoured vehicle.
"Your arrest was invalid so the whole process needs to be backtracked," Chief Justice Umar Ata Bandial told Mr Khan. He would now be under the protection of the Supreme Court.
It was then Mr Khan's turn to speak, defiant and indignant at the way he had been arrested.
The former cricketer told the judges he'd been kidnapped from the High Court on Tuesday and "hit with sticks". He was reminded several times by the judges that others had experienced worse treatment.
There was no immediate response from the security forces to the allegation.