Sen. Paul S. Sarbanes (D-Md.) and the city of Baltimore have gotten the last laugh on the U.S. Department of Housing and Urban Development.

HUD has been trying to collect $16.3 million lent to the city in 1979 under the department's old urban renewal program. Sarbanes added an amendment to the 1983 housing act that would excuse Baltimore from repaying the debt, but HUD called the measure technically flawed and continued to send collection letters to the city.

Several weeks ago, Sarbanes struck again. When the Senate Banking, Housing and Urban Affairs Committee was considering a bill that makes technical adjustments in a number of HUD programs, he inserted another amendment that, in clearer language than the first, wiped Baltimore's debt off the books.

Denver officials, who had been fighting a similar issue in court, went along for the legislative ride. The amendment includes language forgiving Denver's $25 million debt to HUD under the urban renewal program.

President Reagan signed the bill this week.

"We did not prevail on that one," said HUD deputy assistant secretary Timothy L. Coyle. "We felt each city ought to make the repayments." He said HUD would accept the bill because it contains several technical changes that the department and the real estate industry have been seeking.

The controversy centered on Baltimore and Denver because they are two of the only cities that made money under the now-defunct urban renewal program that began in the late 1960s.

That Baltimore owed the money was not in dispute. The city borrowed the money under the Carter administration in order to develop land near its harbor. It signed a contract promising to repay the HUD loan, if it earned enough money by selling the improved land to developers.

The land's value soared as the city completed the nearby Harborplace complex, and a pending sale of downtown land is expected to bring Baltimore more than the $16.3 million that would have to be repaid to HUD. City officials, who have found urban aid grants harder to come by under the Reagan administration, decided to hold onto the money.

They turned to Sarbanes, who got his first amendment enacted into law last November. But HUD officials said the measure cited the wrong subsection of federal law and that they would ignore it.

This touched off a war of words with Senate Democrats, who accused HUD of flouting the will of Congress. Sarbanes, while not conceding the point, added the second amendment to the technical HUD measure, which Congress passed Oct. 3.

"Our effort was to make it so crystal clear that HUD would have no argument," said Bud Moss, a spokesman for Sarbanes. He said the amendment requires that the money be spent on other community development projects in Baltimore.

"This is not an unreasonable action," Moss said. "The city of Baltimore has put this type of money to better use than any other city in the country."

The new language also covers Denver, which has contended that HUD is not entitled to the $25 million generated by its successful Skyline project of downtown offices, shops and housing. Denver officials sued HUD in January, saying that they generated most of the profits by such methods as selling air and underground rights to developers. Denver's urban renewal agency would have run out of money if it had to repay the loans.