A three-judge bench of the Court of Appeal in London ruled that the policy of Al-Hijrah school in Birmingham of compulsory segregation from Class 5 onwards caused detriment and less favourable treatment for both male and female pupils by reason of their sex, and was contrary to the UK's 2010 Equality Act.
The ruling will have an impact on other faith schools that follow similar policies in Britain.
In a test case ruling, the master of the rolls, Sir Terence Etherton, Lady Justice Gloster and Lord Justice Beatson unanimously allowed a challenge by the UK's schools watchdog Ofsted against a previous High Court ruling in favour of the school.
"The school is teaching boys and girls entirely separately, making them walk down separate corridors, and keeping them apart at all times. This is discrimination and is wrong. It places these boys and girls at a disadvantage for life beyond the classroom and the workplace, and fails to prepare them for life in modern Britain," she said.
More From This Section
In November, a High Court judge had overruled the inspectors, saying that they had taken an "erroneous" view on an issue "of considerable public importance".
But in the Court of Appeal ruling, the judges rebuked the UK government and Ofsted for failing to identify the problem earlier and directed that other schools operating similarly should be given time "to put their houses in order".
Around 20 other schools of Islamic, Jewish and Christian denomination are believed to have similar segregation policies in the country, which will now have to be reviewed.
During the appeal hearing, Al-Hijrah's interim executive board had claimed that boys and girls at the school were treated entirely equally despite the segregation, which was attributed to religious reasons.
The school, which is part funded by the state and has pupils aged between four and 16, has complete segregation of boys and girls from ages nine to 16 for all lessons, breaks, school clubs and trips.