Cuban President Fidel Castro abruptly ended his first visit to the United States in 19 years before dawn yesterday, leaving the heavily guarded Cuban mission to the United Nations after 77 hours in New York.

Castro has indicated several times since his arrival that he might stay in the United States for several days more, but Cuban sources said yesterday that Castro had decided to end his visit, even though several appointments had to be canceled.

The officials insisted that Castro was not displeased with anything concerning his New York visit and said in fact that the Cuban leader considered the trip a great success.

They repeated what Castro himself had said on a number of occasions in New York -- that he had come as chairman of the nonaligned movement to give the traditional report to the U.N. General Assembly on movement activities and views.

Castro spent his last night in New York hosting a dinner for high-level U.S. media representatives, including the editors and publishers of several large newspapers and television news personalities. (A story on Saturday night's dinner appears on Page B1).

The dinner originally was suggested by ABC's Barbara Walters but was taken over by the Cubans in keeping with Castro's concern over maintaining contact with the U.S. press. Without normal diplomatic relations with Washington, Castro had utilized the media to transmit messages to the U.S. government and to try to shape U.S. public opinion.

The only noticeably unpleasant moment during Castro's trip came at the outset, when customs officials at Kennedy International Airport kept an angry Castro waiting more than 30 minutes while his party filled out customs cards before he could leave the plane.

The cuban leader was obviously well satisfied with his speech to the United Nations last Friday, a moderate report on the nonaligned summit last month in Havana, and the standing ovation it received.

Perhaps the most remarkable thing about the two-hour address was that it contained none of the harsh anti-U.S. rhetoric Castro has indulged in heavily in recent months in response to Carter administration charges against him. Instead, it concentrated on international economics and the fiscal problems of developing countries.

Castro wanted to present himself as a Third World leader who could rise above bilateral conflicts and according to one Cuban diplomat, "His purpose was accomplished."

The diplomat denied that Castro's ostensibly early departure, which he said was decided in the middle of the night, had anything to do with upcoming U.S. military maneuvers at the U.S. naval base at Guantanamo, Cuba.

Echoing what Castro himself had said about the Marine deployment, which is part of President Carter's response to the presence of a Soviet combat brigade in Cuba, the diplomat said the idea was "laughable."

One reason why Castro may have opted to return home early was the severe restriction heavy security imposed on his movements.

"I'd like to walk around New York," he said in an interview during his flight from Havana Wednesday night. Although the State Department announced that Castro was free to travel wherever he wanted in the United States, the reality of his situation soon became obvious.

While much of Castro's dinner with media personalities and executives was spent in reminiscing and in broad statements on the flow of world affairs, he made several specific points on the controversial question of Cuban troops and experts in Africa and elsewhere:

He said Cuban military personnel, "some advisers, some instructors," are operating in more than a dozen countries around the world, although he denied specifically that there are Cuban troops in Iraq, as has been charged recently.

"I say this honestly," Castro said. "We would prefer at times to send doctors, teachers . . . Cuba is not interested in sending soldiers."

Castro said that he would withdraw the thousands of Cuban troops operating in Angola if a solution is reached to the adjacent South African-controlled territory of Namibia (Southwest Africa).

". . . We are interested in withdrawing military personnel, but there is a question of danger. I don't know if you know that Angola is bombed almost daily," he said in reference to allegations of South African attacks against Namibian guerrilla bases inside Angola.

Cuban troops initially were introduced in Angola during that country's civil war, in which Cuban-backed forces eventually defeated Western and South African-supported factions.

The Cuban leader insisted, however, that with the exceptions of Angola and Ethiopia, where Cuban forces underpin the Marxist government in Addis Ababa, there are "minimum amounts" of Cuban troops or advisers abroad. Referring to a recent map published in the U.S. press that showed countries where Cuba reportedly has more than a hundred troops or advisers, Castro said emphatically that "in none of these places do we have over 100 troops."

He said that in many places, such as Iraq, Cubans are not involved in military projects but are construction workers.

Castro's stay in New York led to one of the city's most intensive security operations, involving thousands of policemen at an estimated cost of about $100,000 a day. A "frozen zone" was established in a four-block area around the Cuban mission to the United Nations and everyone entering the area was required to show identification, including security men.

Both the Secret Service and the New York City police, who received an average of one "serious" threat against Castro's life per hour during the visit, were highly nervous. Any movement outside the Cuban mission was considered to involve tremendous risk, and the activity of hundreds of police and security agents disrupted daily life for thousands of New Yorkers. p

New York police said they were told late Saturday night that Castro would be leaving early the next morning.

Flanked by police cars, Castro's entourage left the Cuban mission on 38th Street at 4:54 a.m. Route to Kennedy Airport already had been blocked off, as had the airport grounds. Flights scheduled to land during the time Castro was to be at the airport were either diverted or forced into a holding pattern. Police helicopters circled overhead.

Castro and his 200 companions and bodyguards left in two Cubana airline jets at 6:04 a.m. for the flight home to Havana.