A five-year battle over the proposed construction of a mosque ended Tuesday when a New Jersey town agreed to settle a pair of lawsuits brought by a local Islamic group and the federal government.

Bernards Township will pay $3.25 million to the Islamic Society of Basking Ridge after the town denied the group a permit to build the mosque, the Justice Department announced. The group can now start building the mosque, and the town will be required to train officials on religious land-use laws and amend its zoning restrictions related to houses of worship.

“Federal law protects people of all religious communities from discrimination and unlawful obstacles when they seek to build a place of worship,” said Tom Wheeler, the acting assistant attorney general for the DOJ’s civil rights division. “Through this agreement, the Islamic Society of Basking Ridge and its members will be able to build a mosque and exercise the fundamental American right of freedom of worship.”

The Islamic Society of Basking Ridge and its members will be able to build a mosque and exercise the fundamental American right of freedom of worship. Tom Wheeler, acting assistant attorney general for the DOJ’s civil rights division

The Islamic Society of Basking Ridge first proposed the construction of the mosque in April 2012. After 39 public hearings over the course of nearly four years — during which the group was targeted by anti-Muslim vandalism, fliers and social media posts — the town voted unanimously in December 2015 to deny the permit, citing concerns over parking spaces and traffic safety.

The Islamic group then sued the town and its planning board in March 2016. The group’s lawsuit alleged that the town’s planning board had manufactured excuses to deny the permit, and fostered “pronounced hostility” against the mosque proposal within the community.

The Justice Department announced in November 2016 that it would also sue the town, alleging the planning board violated a federal religious land-use law by changing its zoning rules. The DOJ also accused the town of using different standards for the Islamic society than it did for other religious groups and of burdening the group’s right to free exercise of religion.

In December, a federal judge ruled that the township’s reasons for rejecting the permit were unconstitutional, and that the town had used vague parking requirements as a tool to prevent the mosque’s construction.

Town officials voted to approve the settlements last week.