President Carter's release of four Puerto Rican terrorists boosted Fidel Castro's presite in the Third World just as he emerged as its dominant pro-Soviet voice, with the Cuban dictator spicing his triumph by violating one commitment to Washington and going slow on another.

Castro privately offered early this year to swap four Americans jailed in Cuba in return for the four Puerto Ricans held in U.S. jails, promising not to publicly claim credit. The Americans were still in jail five days after the nationalists were released. Castro immediately broke his promise of discretion, in effect proclaiming himself the hero of Puerto Rico.

So the commander of Soviet surrogate troops in Africa and leader of spreading Marxist revolution in the Caribbean and Central America is lionized in the Third World, thanks to U.S. policies. This turn of events is strange even in the wonderworld of Carter administration foreign policy. How did it happen?

The president's decision stemmed not only from a simplistic commitment to "human rights" but also from the desire of some State Department officials to head off further diatribes against the United States at the Castro-dominated "non-aligned" conference in Havana (which denounced Yankee imperialism anyway). And there is strong evidence Carter was thinking in terms of his own domestic political interests as well.

Although the administration denies any tit-for-tat deal, the original proposal came from Castro himself early this year when a congressional delegation went to Havana to seek release of the Americans. "There need be no negotiations or publicity," Castro privately informed the congressmen. "You've made an appeal and I have proposed a solution."

Carter quickly bought the deal, supported by the State Department's human-rights specialists, but amid doubts by his principal foreign-policy aides.

The doubts were quickly confirmed. Despite Castro's implicit pledge of silence, this headline appeared in the controlled Havana press only hours after the release of the terrorists: "Carter Forced to Release Puerto Ricans." That raised well-founded suspicions that the wily Castro had always intended to parade his conquest over Washington during the non-aligned conference.

Nor was Castro in any rush to fulfill his end of the bargain. At this writing, Washington still expects the release of the Americans, but there has been no move from Havana.

The certainty that releasing the four terrorists would add further glitter to Castro as revolutionary leader prompted a warning to Carter earlier this year from Puerto Rico's Gov. Carlos Romero-Barcelo. "Adversaries of the United States," he said, referring obliquely to Castro, "will interpret the gesture as a tacit admission by the United States that . . . Puerto Rico's role as a part of the United States is both invalid and intolerable." Unless the four terrorists admit their guilt, the governor added, their release will justify charges that they have been held all these years as "political" prisoners.

Castro has been trumpeting exactly that. The terrorists were "political prisoners," and Puerto Rico, like pre-revolutionary Nicaragua, is a captive of the "colonial" power of the United States.

When the four Puerto Ricans walked out of jail, they threatened to repeat the criminal acts that put them there following the assassination attempt on President Truman and the shoot-up of the House of Representatives. Back in San Juan, they were received as heroes, then went on an island-wide tour reviling the United States and praising Castro.

Simultaneously, on Sept. 13 at a Washington dinner for several hundred leaders of the Hispanic community in this country, Carter claimed political credit for the release of the Puerto Ricans. But it is doubtful that that will gain him the support of any Hispanic Americans.

As of now, not even native Puerto Ricans want independence. In repeated votes, the Independence Party has received negligible support. But that might change with Castro able to claim U.S. recognition as unofficial bargaining agent for Puerto Rican independence.

This performance, coming while Washington seems powerless to push Soviet combat troops off Cuba, suggests the perils of playing to the applause of the Third World in shaping U.S. foreign policy. Those perils are deepened when combined with understandable efforts of an unpopular president to save himself politically.