The administrative court in Düsseldorf ruled that the state's responsibility to educate the girl outweighed the potential infringement on her religious freedom.
The 12-year-old girl's parents sued her school in the northern German city of Remscheid after the school refused to allow the girl to skip swimming class. Citing religious reasons, the parents said they did not want their daughter to participate because her body would not be covered.
The ruling means the girl must participate in swimming lessons starting next week, Sabine Ernst of the Alexander von Humboldt School told German news agency DDP. Ernst declined to state what the consequences could be if the girl declines to attend.
Her parents' attorney said the family plans to appeal and that the decision should not be considered precedent-setting.
The parents rejected an offer last year from the girl's teacher to allow her to swim in leggings and a t-shirt, saying the girl's body would still be visible through the wet clothing.
The Düsseldorf court concluded that because most of a swimming lesson takes place underwater, the girl's body would largely remain obscured even in wet clothing.
The Remscheid school said it did not have the personnel to separate boys and girls in swimming lessons. In the past the school had allowed Muslim girls to skip the lessons and take a different class instead, but a rule put in place last year explicitly requires participation by Muslim students.