The House narrowly defeated a challenge yesterday to a bill necessary to put the Panama Canal treaties into effect, and House leaders, recognizing that the legislation is in danger, hastily postponed further action for at least two weeks.

In what amounted to a test vote on the legislation to implement and finance the transfer of the canal to Panama, backers of the bill were able to muster only a two-vote margin to approve a rule spelling out the procedures for House debate on the measure.

The unexpected closeness of the 200-to-198 vote caused House Speaker Thomas P. (Tip) O'Neill Jr. (D-Mass.) to reshuffle plans for bringing the bill to a vote next week.

"It's been pulled because we've got some work to do," O'Neill said of the legislation that the Carter administration considers necessary to honor its controversial treaties with Panama and ward off a possible crisis in U.S. relations with Latin American.

Rep. John Murphy (D-N.Y.), chairman of the House Merchant Marine Committee, was more bluntly pessimistic about the bill's current chances of passage. He said, "We anticipated 180 votes against. This indicated there's a much stronger feeling against the bill than we thought."

Murphy blamed the opposition on what he charged were "inaccurate statements" by the administration on the costs of transferring the canal, on recent charges that Panama has been smuggling arms to leftist guerrillas in Nicaragua and on what he called "fallacious statements" by State Department spokesman Hodding Carter that the legislation violates the spirit of the treaties.

His comments underscored the fact that Murphy and the administration are, at best, reluctant and uneasy allies in the drive to have the legislation passed. Muprhy, an outspoken critic of many features in the treaties, was responsible for amending the authorizing legislation originally proposed to the House by the administration in ways that drew objections from the State Department.

That was why Hodding Carter, in an April 6 statement, called some of Murphy's proposals inconsistent with the letter and spirit of the treaties. Since then, however, the administration has come around to the view that Murphy's version of the legislation is the best it can hope for, and, despite its reservations, the administration now supports passage of his bill.

However, as yesterday's vote showed, the administration and its House backers can't even be sure of getting the Murphy bill passed. Instead, there is now a real threat that the House will reject the legislation outright or load it with crippling amendments that would be unacceptable to Panama.

Among the things that the legislation would do is provide funds and authorization for the mixed U.S. Panamanian commission that will run the canal until the end of the century, for the initial turnover of U.S. canal zone property to Panama and for the payment of Panama of profits from canal tolls.

The troubled outlook for the legislation is the result of a confluence of forces that have their roots in conservative opposition to surrendering U.S. control over the canal and the resulting bitter debate about the treaties that ended when the Senate approved them last year by a single vote.

Although many House members argued at the time that they too should vote on the treaties, the administration prevailed in its argument that it was a matter for the Senate alone to decide. Now the resentments and frustrations built up when the House was sidetracked from last year's debate have burst out anew as the result of the authorizing legisltion that gives the House a handle.

The chief argument made by the opponents is that the administration consistently has concealed from the American public the cost of transferring the canal. Various opponents have charged that the United States will be surrendering land and propoerty that some say is worth from $10 billion to $14 billion and that additional transfer costs could run as high as $4 billion.

These figures are disputed by the State Department as highly inaccurate and distorted. State officials, while conceding that many different accounting methods can be used, estimate the value of canal zone property being surrendered to Panama at slightly more than $319 million.

They note that the United States has had the use of this property for more than 70 years, that the Senate, in approving the treaties, agreed to give it away and that the transfer of ownership to Panama of this amortized property will not cost the U.S. taxpayers any additional outlays.

According to the department's arguments, the only legitimate issue involves those additional costs that the United States will have to assume in order to carry out the treaty provisions for transferring the canal. These involve primarily the relocation of U.S. military equipment and facilities from the canal zone and certain moving and pension costs for American employes of the canal who will be displaced.

Originally, the administration estimated these costs at about $350 million between now and the end of the century. More recently, the State Department has upped this estimate to $870 million, and, while conceding that this is more than double the original estimate, department officials say it is a far more accurate projection than the $4 billion talked of by some opponents.

Although arguments about finances have dominated the House debate, Murphy and other House members said yesterday there also are lingering suspicions on Capitol Hill about possible secret agreements made by the administration with the Panamanian government, which many conservatives regard as overly leftist. The administration has vehemently denied any secret agreements.

In addition, Murphy said, many conservative members apparently have been influenced heavily by charges that Panamanian officials have shipped illegal arms to leftist guerrillas fighting against Nicaragua's President Anastasio Somoza. Despite the severe strains between Somoza and the Carter administration, he is supported by many influential members of Congress, including Murphy.

Although the Panamanian government is outspokenly hostile to Somoza, Panama's President Aristides Royo, in a visit here last week, denied the arms-smuggling charges. State Department officials say they regard his denials as convincing.