After three years of often-rancorous debate, Congress last week agreed on a way to save hundreds of thousands of rental apartments for low-income people without penalizing owners of the property.

The complex measure, part of a housing bill passed in the last hours before Congress adjourned last Saturday, will enable most poor residents of about 360,000 apartments nationwide to stay where they are. Many of the buildings are located in cities with acute shortages of low-cost housing.

Preserving the buildings for low- and moderate-income residents will cost the government $27.4 billion over the next 20 years, the Office of Management and Budget estimates. Supporters of the legislation said replacing the housing would be much more costly.

President Bush is expected to sign the bill into law.

Congress passed a stopgap law in 1987 to preserve most of the housing and has been working on a permanent solution since then. Tenant advocates argued that the properties should be saved for low- and moderate-income residents. Property owners said they had a right to do what they want with their buildings after meeting a 20-year obligation under the old law.

The new law targets buildings constructed between 1961 and 1973 by private owners who received federally subsidized loans with interest rates of 1 percent or 3 percent. In some cases, additional subsidies were available for tenants. In return for the federal aid, owners agreed to keep modest rents for 20 years, but were free then to sell the buildings, convert them to condominiums or raise the rents.

When the programs were conceived, however, no provisions were included to replace the housing or relocate the tenants at the end of 20 years. But in the last few years, housing organizations and federal officials began to realize that thousands of poor tenants could be displaced because of what a former government executive called "a blind spot about what would happen down the road."

The new law provides for government incentives to push owners' income up to fair market rental rates. In return, an owner must maintain affordable rents for the "remaining useful life" of the property or sell it to a buyer who will. If an owner sells, the government guarantees a fair sales price, based on an independent appraisal, and will help nonprofit groups or public agencies buy the building.

Only in a few cases under the law will owners be able to pay off their mortgages and sell buildings without restrictions, most housing experts said. These would be in very high cost areas, such as New York City and some areas of California, they said. Some estimates put the number of vulnerable units at between 6,000 and 9,000.

In these instances, tenants in areas where vacancy rates are low could stay in their apartments for three years after the sales. Residents with disabilities or with other special needs and the elderly could remain in their homes for three years in all areas.

A provision that building owners lobbied hard for preempts state and local laws that would restrict owners from selling their property.

With a few reservations, both sides say they are happy with the new law.

The legislation is "a great example of how conflict is resolved by government," according to Frank T. DeStefano, staff director for the House subcommittee responsible for the law. Congress needed to preserve private property rights while protecting federal investment in the buildings, he said. Along with interest subsidies, the government's costs will include rental assistance for tenants in some cases and valuable tax breaks for owners, he said.

Congress wanted to reach a long-term solution this session after "three years of agonizing debate," said James H. Schuyler, an attorney and housing expert. Members of Congress expected long court battles if a compromise were not reached because owners said that changing the rules violated their constitutional rights, he said.

But Barry Zigas, president of the National Low Income Housing Coalition, said the multimillion-dollar cost of the legislation is the "down side."

"It's a lot of money that will not be available to people who don't already live in affordable housing," Zigas said. "It's unfortunate that a mistake the federal government made 20 years ago ... will enrich so many owners who've already received so much" from the government.

Still, Zigas and other low-income housing advocates applauded the changes.

"It is a really balanced" law, said Sara Johnson, head of an organization established to work for preservation of the housing. Buildings could be removed from low-income use under some circumstances, but generally the law "is a more than fair result for the owners and it doesn't necessarily stick state and local governments with the cost."

Officials at the Department of Housing and Urban Development, which will implement the legislation, see the new law as providing a new opportunity to further the Bush administration's goals of selling low-income housing to tenants, according to Mary S. Brunette, HUD assistant secretary for public affairs. Implementing the new law "will be a very high priority" for HUD, where officials are "already working" on the regulations, she said.

Although the legislative solution is "reasonable and fair," much of its effectiveness will depend on how it's administered, said Eugene Ford, a Washington-area developer and head of a building owners' organization. HUD's area offices have been so "decimated" that the department's enforcement may be ineffective, he said.

William Kargman, president of the National Assisted Housing Management Association, said he welcomed most provisions of the law, but is "a little disappointed that there is a very strong emphasis on sale to nonprofits," he said. "There are many owners who are good managers who would like to stay in and operate the housing."