The federal high court in Abuja said Ibrahim Zakzaky, who heads the Islamic Movement of Nigeria (IMN) and his wife should be released within 45 days and ordered the government to provide them with a new home following the demolition of their residence during the violence.
"The applicant is entitled to have judgement enter in his favour," said Judge Gabriel Kolawole in his ruling on Zakzaky's legal challenge against his detention.
The judge also dismissed the argument of the prosecution and secret police that the Islamic cleric was being kept in "protective custody".
"The applicant shall be released within 45 days from today," he said, adding that he should be given a "proper and decent" accommodation in a place of his choice in northern Nigeria.
More From This Section
The judge also awarded 25 million naira (USD 79,000) each to Zakzaky and his wife for unlawful detention.
The couple were arrested on December 15, 2015 after two days of bloody clashes between his supporters and soldiers which led to the death of over 300 people, including his two children.
The fighting began on December 12 when supporters of the pro-Iranian cleric refused to allow the chief of army staff's convoy to pass through the northern city of Zaria in Kaduna state.
In April, rights group Amnesty International accused Nigeria's military of shooting dead some 350 Shiite Muslims, burying them in mass graves and destroying evidence of the crime.
He has previously been imprisoned for calling for an Iranian-style revolution to create an Islamic state in the country's north.