A High Court judge has criticised the home secretary after hearing Muslim men were forced to pray next to a toilet at an immigration removal centre.
Mr Justice Holman said Amber Rudd had failed to provide any justification for the practice at the Brook House facility, near Gatwick Airport - operated by the private company, G4S.
As a result, he said the two detainees' rights had been breached.
The Home Office said it would consider the judgement "carefully."
The judge also ruled Ms Rudd had acted unlawfully in allowing smoking in rooms at the centre.
Regulations permitting exemptions to the smoking ban in public places do not extend to immigration removal centres, said Mr Justice Holman, but could be amended if the government wished to do so.
The ruling means the home secretary will either have to phase out smoking from most areas of UK immigration removal centres or get the Health Secretary Jeremy Hunt to agree to introduce new rules allowing it.
The case against Ms Rudd was brought by two asylum seekers - Mohammed Hussein, originally from Ethiopia, and Muhammad Rahman, from Bangladesh.
They were both detained at Brook House for several months last year before being released.
The pair, who said they were devout Muslims, argued that the conditions and regime at the centre breached their right to religious observance.
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 3m 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.
Centres 'fully equipped'
A Home Office spokeswoman said the department respected the rights of detainees to practise their religious faith.
"Immigration Removal Centres are equipped with mosques and multi faith rooms for detainees to use for prayer, study and reflection," she said.
"Communal prayers are available in all centres as well as facilities for prayer in the detainees' rooms such as access to prayer mats."
But she added: "We will consider today's judgement carefully."
Duncan Lewis Solicitors, who represented the detainees, said they hoped the government would take immediate steps to improve conditions.
Raza Halim, the pair's barrister, said: "Religion and prayer provide unique solace to many far from home because of war or persecution.
"It is particularly profound in the circumstances of a detention that you cannot understand.
"The court recognised how Muslim worship could be 'degraded' because of where detainees are forced to pray near open, recently used toilet pans in small cells."
The BBC has contacted G4S for a response.
Brook House became the subject of a BBC Panorama undercover investigation last year that revealed detainees were "mocked and abused".
The programme said it had seen "widespread self-harm and attempted suicides" in the centre, and that drug use was "rife".
According to the Home Office, the majority of those held are failed asylum seekers or illegal immigrants waiting to be deported from the UK on organised charter flights.
Brook House is one of 11 detention removal centres in England, which together took in 28,908 people last year - including 71 children.