President Carter yesterday granted freedom to four Puerto Rican nationalists who have been imprisoned since the 1950s for terrorist attacks on American politicians.

A White House announcement said the granting of clemency was recommended by Attorney General Benjamin R. Civiletti and Secretary of State Cyrus R. Vance.

Civiletti noted that each prisoner had served more than 25 years, far longer than most criminals, and seemed to pose little risk of further illegal activity or "becoming the rallying point for terrorist groups."

Vance said the release "would be a significant humanitarian gesture and would be viewed as such by much of the international community."

One of the prisoners, Oscar Collazo, 67, was serving a life term for an assassination attempt on President Truman in 1950 in which a White House guard and another Puerto Rican gunman were killed.

The other three prisoners -- Lolita Lebron, Irving Flores Rodriguez and Rafael Concel Miranda -- wounded five House members in a shooting attack from the visitors' gallery in 1954.

All four have been viewed as political prisoners by governments favoring Puerto Rican independence. Puerto Rico Gov. Carlos Romero Barcelo, who favors statehood rather than independence, for Puerto Rico, a commonwealth of the United States, had opposed their release because of concern they would return to the island and stir up nationalist feelings.

J. A. Marquez, director of Puerto Rico's office here, said in a recent interview that Romero wonders "who will be responsible" if the prisoners return.

In a statement yesterday, Marquez said: "Although I do not agree with their philosophy . . . I am happy that the four men have been released and hope that they may be rejoined with their families and lead peaceful and productive lives."

A Justice Department spokesman said the four prisoners could be released by Monday.

The prisoners' commitment to their cause has been so strong they refused to apply for parole bedause they do not recognize the sovereignty of the United States over themselves or Puerto Rico.

Collazo has been eligible for parole since 1966, and Lebron since 1969.

Rep. Robert Garcia (D-N.Y.), who worked for the prisoners' release, said yesterday that he was delighted with the announcement. He noted that he grew up next door to Collazo's family in the South Bronx.

In Havana, Juan Mari Bras, head of the Puerto Rican Socialist Party, called Carter's announcement "a tardy carrying out of an elemental act of justice." Mari Bras was in Cuba for the nonaligned summit conference, where earlier in the day he had called for the prisoners' release.

Cuban President Fidel Castro has continually called for Puerto Rican independence and told visiting U.S. congressmen in Havana that he would free four Americans held in Cuba if the four Puerto Ricans were released.

One Justice Department official involved in the recommendation of their release emphasized that there was no connection between yesterday's announcement by President Carter and any decision by Castro to free four Americans held since the 1960s.

"This case [the Puerto Ricans] was decided on its own merits," the official said. "If something else comes out of it, that's a bonus."

The four Americans mentioned by Castro are Lawrence Lunt, who was sentenced in 1965 for spying; Juan Tur, sentenced for espionage in 1964; Everett Jackson, sentenced to 30 years in 1968, and Claudio Morales, a Puerto Rican who was imprisoned after he infringed on Cuban territorial waters in 1965.