President Carter narrowly avoided a damaging defeat yesterday as the House Commerce Committee rejected by a 21-to-21 vote an effort to block his decision to decontrol domestic oil prices.

The White House had lobbied hard against the assault on Carter's plan to reduce oil use and increase production by rising prices, and the administration expected to win easily. Opponents of decontrol, led by Rep. Toby Moffett (D-Conn.), said their overnight gain of three or four votes showed that public opposition to the president's plan is gathering momentum and that the issue will not go away. Democrats voted 18 to 9 against Carter.

Speaker Thomas P.(Tip) O'Neill Jr. (D-Mass.), a loyal supporter of the president who has limited his comments on the issue to speaking up for a stronger "windfall profits" tax on the oil companies to soak up part of the price increase, said yesterday that if he had to vote, he "would have voted with Moffett." Not many members from oil-consuming northeastern states would vote to lift oil price controls, he said.

O'neill said he thought the issue would be very close if brought to a House vote, but said he could not conceive of the Senate voting to keep controls. He said that while he would not work for Moffet, he will not work against him either.

Moffett vowed that "we'll have more votes on this." He said opponetns of decontrol will ask the caucus of all House Democrats to take a stand and promised a vote by the full House.

White House lobbyist William H. Cable said the administration at one point thought it had 24 votes against Moffett. "clearly, there is a frustration over higher oil prices," said Cable. "The president shares that frustration," he said, but doesn't think continued price controls are the solution to growing dependence on costly foreign oil.

Cable said the Commerce Committee is the most sympathetic forum in the House for opponents of decontrol. "If they can't it here, I don't think they would have the votes" in the full House, he said.

Moffett attempted to add his proposal to the Department of Energy's annual authorization bill, as a prohibition against spending money to administer decontrol. Rep. John Dingell (D-Mich.), subcommittee chariman managing the bill and a staunch opponent in the last Congress of decontrolling natural gas, argued that Moffett was offering it to the wrong bill and the Senate wouldn't accept it.

Moffett urged the committee not to be swayed by predictions about the Senate. "Public anger will catch up with this place," he predicted.

Rep. Matthew Rinaldo (N.J.), one of the Republicans voting against decontrol, said that for him the issue came down to a matter of faith. He said his constituents don't have faith that the oil companies would use higher prices to find more oil. The other Republicans opposing decontrol were Reps. Tim Lee Carter (Ky.) and Mark Marks (Pa.).

Most Republicans and some Democrats argued that five years of price controls have led only to lower domestic production and tripling of oil imports and quadrupling of foreign prices. This proves, they argued, that domestic price controls have been a failure.

In other congressional action, the Senate approved, 89 to 3, a Carter standby energy conservation plan that would empower him to order that thermostats in nonresidential buildings be set no higher than 65 degrees in winter and no lower than 80 degrees in summer. On the recommendation of its Energy Committee, the Senate rejected, 70 to 23, a second Carter plan that would have let him ban outdoor advertising lights. The committee said this would save only a trifling amount of energy.