Mr Justice Holman said Amber Rudd had failed to provide any legitimate reasoning for the practice at the Brook House facility near Gatwick Airport, which is operated by the private company G4S.
As a result, he said the two Muslim detainees’ rights had been breached.
The Home Office said it would consider the judgement “carefully”.
The case against Ms Rudd was brought by two asylum seekers – Muhammad Rahman from Bangladesh and Mohammed Hussein who is originally from Ethiopia.
They were both detained at Brook House for several months in 2017 before being released.
Both men, who said they were practicing Muslims, argued that the conditions and routine at the centre breached their right to religious observance.
Subscribe to our newsletter and stay updated on the latest news and updates from around the Muslim world!
They said they were each locked in a room – measuring 4m x 3m – for 11 hours at night with two other detainees, and their evening and early morning prayers were conducted 3 metres from an “exposed and open lavatory pan”.
One of the men described the room as “smelly and dirty”, saying it distracted him from his prayers.
The other detainee called the conditions “disgusting”.
Mr Justice Holman said the evidence “clearly established” that being forced to worship in such a confined space with an internal lavatory contravened the men’s right to worship under Article 9 of the European Convention on Human Rights.
He said the conditions were also discriminatory because they affected Muslims more than people of other religions due to their prayer requirements.
The court heard that the home secretary had “conceded” that she had not paid “due regard” to the need to eliminate discrimination as she was obliged to do under the Equality Act.
The judge ordered her to address the issue within a “reasonable time” and demonstrate that she had done so.
A Home Office spokeswoman said: “Immigration Removal Centres are equipped with mosques and multi faith rooms for detainees to use for prayer, study and reflection.
“Communal prayers are available in all centres as well as facilities for prayer in the detainees’ rooms such as access to prayer mats.
“We will consider today’s judgement carefully.”
G4S have not commented on this case.