Panama was elected to a two-year term on the Security Council today, a development that demonstrated Cuba's residual power in the international community and served as a warning to the Reagan administration not to renege on the American commitment to the canal treaties.

Today's vote by the General Assembley -- 111 for Panama to 16 for Costa Rica -- was a formality after the Costa Rican ambassador announced his country's withdrawal yesterday from the bitter contest.

The battle over this Latin American seat in the council had begun months ago as a test of wills between Costa Rica and Cuba. c

Although the Cubans recognized from the start that they could not win the two-thirds majority required and dropped out as a candidate, they were able to muster a blocking third from among their Soviet bloc and Third World allies to deny Costa Rica a victory through 22 inconclusive secret ballots.

The Cuban motive, according to Latin American diplomats, was to pay the Americans and Costa Ricans back for their successful effort to prevent Cuba's election to another council seat one year ago. That was a marathon contest which ran to 154 ballots and ended this past January with the election of Mexico to the second Latin American seat on the 15-nation council.

"Havana showed today that it cannot be taken for granted," one Latin American diplomat admitted. "Governments will now think twice before publicly challenging the Cubans."

Costa Rica came within one vote of victory in the early balloting, and the surrogate candidates put up by the Cubans, such as Guyana and Nicaragua, never won wide support.

Costa Rica maintained its broad backing even when the Cuban votes were switched to Panama, because Costa Rica remained the only active Latin American candidate. Panamanian representative Jorge Enrique Illueca insisted that his government was not seeking the Council seat.

But after Ronald Reagan's election, Illueca told a closed meeting of Latin American delegates that his government had decided to pursue the council seat actively and to use it as leverage should the new administration in Washington fail to live up to the terms of the canal treaties.

The result was that some 30 Third World nations shifted from Costa Rica to Panama, giving it a lead of 93 to 52 on the penultimate ballot yesterday, just five votes short of victory.

At that point, Costa Rica's Rodolfo Piza Escalante announced to the assembly that his country would no longer stand as the "victim of orchestrated maneuvers" by a "well-tooled" minority of nations.

He warned, however, that the Cuban power play could serve as a dangerous precedent to threaten the gentlemen's agreement that has stood for three decades under which each regional group selects its council representatives without interference from outside blocs. There are five permanent members of the council who hold veto power and the remaining 10 members are elected for rotating terms by the General Assembly.

The upshot is that Panama will succeed Jamaica in the council and take its seat on Jan. 1, along with Uganda, Japan, Ireland and Spain, which were elected to two-year terms earlier without opposition.