The court ruled that Khan’s arrest on Tuesday was illegal and said he should be released. Khan’s arrest has sparked violence in major Pakistani cities.