The handshake requirement will deter few applicants for citizenship, officials said, but it sends a harsh message to Muslims, and many mayors who conduct citizenship ceremonies said they would find ways to avoid it.

“It’s against my ideology and conviction to have to force other people to have body contact,” said Thomas Andresen, the mayor of Aabenraa, near the border with Germany.

To circumvent the law, Mr. Andresen said, he could either arrange to have local officials of both genders take part in the ceremonies or have state officials take over. Either way, he said, he would look for pragmatic solutions while protesting legislation “gone too far.”

Mogens Jespersen, the mayor of Mariagerfjord, a northern town, told the national broadcaster that he would disregard the law and accept a nod or a bow from an applicant refusing to extend her hand.

“But I think it’s a hypothetical question I’ll never face,” he said.

In Ishoj, a suburb of Copenhagen, immigrants and their descendants constitute 40 percent of the population. But its mayor of 17 years, Ole Bjorstorp, said he had never met anybody who refused to shake his hand.

If it were to happen, he said, he would just find somebody else to perform the ceremony.

“My conscience doesn’t allow me to report people on this basis so they won’t become Danish citizens,” he said. “It’s a moral and ethical issue.”

One mayor said he had met a female asylum seeker who declined to shake his hand for religious reasons, but he did not find it disturbing.