The United States and Panama agreed today on a treaty that would grant Panama control over the Panama Canal and the Canal Zone by the year 2000 but also give the United States the right to defend the waterway indefinitely.

Although details were not available, the agreement would - if ratified by the U.S. Senate - put an end to 73 years of U.S. control over a Panamanian enclave in which it carved a lock canal linking the Atlantic and Pacific oceans, one of the engineering wonders of the world.

Before the announcement, which came at 7:20 p.m. EDT there was a total news blackout. But as the final agreement came closer, officials would scurry from meeting rooms and warn journalists that a new treaty was perhaps two hours, one hour then 30 minutes away.

Head American negotiator Ellsworth Bunker said he was "deeply gratified" after finishing the "long and arduous task."

"From the point of view of the United States, we are confident that this treaty will not only protect but strengthen our nation's security interest," he added in his prepared statement.

Romulo Escobar chief Panamanian negotiator, said he was pleased with the agreement, which would eliminate the "odious or hateful perpetuity that prevented our country from becoming one."

Bunker announced that he and Sol Linowitz, the other American negotiator would fly to Washington Thursday and meet immediately with President Carter.

For Carter, the new treaty could mean a cementing of his relations with Latin America, above all with liberals and civilian ruled regimes that have endorsed his controversial human-rights campaign.

At a meeting last weekend of the leaders of Colombia, Venezuela, Mexico, Costa Rica, Jamaica and Panama, tribute was paid to the rapid progress made in the long treaty negotiations since Carter took office, Colombia's President Alfonso Lopez Michelsen even proclaimed that Carter fell into the tradition of "progressive and humanist Presidents like Abraham Lincoln and Franklin D. Roosevelt."

For Panama leader Gen. Omar Torrijos, the long-awaited agreement should bring a needed boost to his popularity among Panamanians, whose attention in recent years has veered from the canal issue to the serious economic crisis at home. An end to this crisis depends almost solely on the new treaty, which is expected to bring back stalled domestic and foreign investment.

Almost single-handedly since he seized power in a bloodless coup Oct. 11, 1968, the general mobilized domestic opinion in favor of a new treaty and variously threatened and cajoled Washington to abandon its perpetual control over the canal and Canal Zone as given in the 1903 accord.

Torrijos, 49, also drew world attention to what he described as the "colonial enclave" maintained by the United States in the Canal Zone. At the U.N. Security Council meeting in Panama in 1973 and in innumerable international and inter-American forums, he won strong resolutions calling on Washington to conclude a new treaty with Panama.

But both leaders now face tough battles to insure the ratification of the new treaty - by a two-thirds majority in the American Senate and by overwhelming majority in a plebiscite later this summer by the Panamanian people.

Carter aware that close to a third of the Senate is already on record as opposing "giving away" the canal, urged all congressmen this week not to prejudge the new treaty until they had seen it in all its details.

Torrijos, on the other hand, has already launched a campaign to whip up popular enthusiasm for the agreement and to drown out complaints from small opposition groups that the new treaty is not as favorable to Panama as anticipated.

He controls all the media here and has refused to allow the return of several dozen outspoken leftist and rightist exiles who would be expected to try to swing public opinion against the treaty and against the strongman himself.

At the same time, Torrijos must allow a sufficiently open debate to forestall complaints from American congressmen that the treaty is being signed with an authoritarian dictator.

Lawyers' group in Panama are already opposing what they call the "legalization" of 14 U.S. miltary bases in the zone, which are not provided for in the 1903 treaty. Under that agreement the United States was granted the right to protect the canal, which the lawyers say did not include a permanent right to maintain troops here or establish military training schools not related to the canal defense.

During the final days of negotiations, however, Panamanians in general appeared to have little idea of what was being discussed on the eighth floor of the American Holiday Inn overlooking Panama Bay. Many were astonished to hear that the longawaited treaty was finally being concluded.

The only evidence that this was indeed the final round of talks was provided by dozens of reporters and television teams camped in the lobby of the hotel for the past three days.

Part of the delay - at least four times it was announced that in accord was expected and then postponed for several more hours - came after broad conceptual agreement had been reached, from relatively minor problems and in polishing the language in both English and Spanish.

One Panamanian negotiator explained that one side, for example, would want to change a word to make it politically more palatable, and then the other side would say the new word made a fundamental change like the difference between compensation and indemnification.

One United State translator who worked for long hours explained "by the time you get from 'probably' to 'probablementre,' and then from 'hypothetically' to 'hypoteticamente', you may have changed the original meaning of the draft. And there are legal terms which look the same on paper in both languages, but when you interpret them they are totally different."

Sources close to the negotiations explained that under the pressure to meet today's deadline, negotiators would trade off concessions.

If history has been made here, as Linowitz described it, the setting for the lofty phrasing has been prosaic at best. The long, narrow negotiating table of standard hotel vintage had none of the dignity of the sweeping polished tabletops seen on such occasions in government palaces.