Mr. Behr and his wife, Barbara, stand in front of the town hall of Somerset in 1997. (Tom Allen/The Washington Post)

Walter J. Behr, who served for nearly 30 years as mayor of Somerset, a picturesque Montgomery County enclave he helped shield from development — most publicly by overseeing the expulsion of high-rise condominiums from town borders — died Jan. 2 at his home there. He was 97.

The cause was congestive heart failure, said his son, William A. Behr.

Mr. Behr spent most of his working life as the Washington-area sales manager of Family Record Plan, a photo services company.

He had an equally extensive career as a public official in Somerset, an incorporated town of single-family homes and tree-lined, narrow streets within the borders of Chevy Chase. The oldest homes — wood-frame Victorians with broad porches — reflect the town’s heritage as a trolley suburb of Washington in the late 19th century.

As the town’s longest-serving mayor — he was nearing 90 when he retired from public life — Mr. Behr viewed himself as a “hands-on” caretaker who brooked no dissent on Somerset’s strict tree and fence ordinances and its enforcement of building, animal control and noise laws.

Zola Dincin Schneider, who moved to the town in the 1960s, said Mr. Behr “was someone who could take all the outrageous display of opinions and seemingly not let it wound him.”

Mr. Behr’s most public battles involved how to preserve the town’s idyllic identity from encroaching commercialism.

Starting in the late 1970s, he worked with future congresswoman Constance Morella and other state and federal politicians on successful efforts to buy parcels of wooded land for hundreds of thousands of dollars.

That green space acted as a buffer from commercial development in Bethesda to the north and Washington’s Friendship Heights neighborhood to the south.

Developers had planned for decades to build luxury high-rise apartments on the town’s southern border — to be called Somerset House. After a long legal battle with Somerset activists, developers prevailed in the late 1980s, and town officials began to discuss the possibility of “de-annexing” the land.

Somerset residents weighed the loss of sizable tax revenue against concerns about changing the town’s aesthetic character. In 1988, residents overwhelmingly voted to de-annex the 18 acres that included the Somerset House property.

A motivating factor was a belief that the many hundreds of new apartment dwellers would dilute the political power of Somerset’s roughly 1,100 homeowners and gain too much influence over town affairs.

“We just thought that residents of high-rises would have different goals and priorities and could take over town government,” Mr. Behr told The Washington Post. “Their complex has a very nice swimming pool, so what if they decided that our community pool wasn’t necessary and voted to let it run down? These were worries of ours.”

Under Mr. Behr’s mayoral watch, parts of the town won county designation as a historic district, in essence ensuring the preservation of dozens of homes. But a recent trend toward mansionization — building grandiose structures on one-acre parcels — has since become an issue of contention within town borders.

Walter Julian Behr was born in Cincinnati on Aug. 6, 1917. He was a 1939 economics graduate of Oberlin College in Ohio and spent part of his early career as a labor economist, becoming a division director with the now-defunct federal Wage Stabilization Board.

During World War II, he served in the Army. He edited a daily mimeographed newspaper in Assam, India, and later served as an information and education officer in the United States after attending Officer Candidate School.

His first wife, Louise Frieder, whom he married in 1945, died in 1987. Their son Thomas died as a child in 1954.

Survivors include his wife of 24 years, Barbara Bradshaw of Somerset; four children from his first marriage, William A. Behr of New York, Edith Behr of West Orange, N.J., Sarah Moaba of Millburn, N.J., and Margaret Laird of Williamsburg, Mass.; a stepson, Kenneth Bradshaw of Cross Junction, Va.; nine grandchildren; two great-grandchildren.

Mr. Behr settled in Somerset in 1957 and grew active in town politics through his leadership in a parent-teacher organization. He won election to the Town Council in 1967, then served as mayor from 1975 to 1982 and again from 1986 to 2008. He received a small stipend for the job.

He helped start an after-school program with Whittier Elementary School in Washington, bringing together the mostly white student body in Somerset with the black students of Whittier.

Mr. Behr was a past president of Temple Sinai, a Reform synagogue in Washington.

He was a frequent presence on the Somerset tennis courts, where he was fond of playing doubles.

Lesley Simmons, a former Town Council member and town historian, said Mr. Behr’s talent was balancing firmness with diplomacy when working to resolve community disputes, whether over barking dogs or stray tree limbs above fences.

“He was very fair and open and just stuck to the rules,” Simmons said, recalling Mr. Behr’s frequent intervention in disputes. “That’s hard to do in a community where your neighbors and friends are those you’re saying no to.”