Mexican President Jose Lopez Portillo joined Cuban President Fidel Castro today to urge the United States to end its 15-year economic blocade of Cuba and to remove its military base on the island.

The two presidents issued a communique as Castro ended his two-day visit here. It "demanded the end of all acts of economic aggression against these countries [Latin America] because they violate the peaceful coexistence between nations and the principles and objectives of the United Nations.

In a marathon press conference, which began Thursday and ran past midnight, Casto also attacked the U.S. boycott as a contridiction of the Carter administration's human rights policy. He also declared that Cuba has not intervended in Nicaragua while the United States has "traditionally intervented in Central America."

Castro, after arriving Thursday for a two-day visit to Mexico-his first since seizing power 20 years ago-declared that "President Carter's preachings about human rights do not even leave us the opportunity to buy an aspirin for a toothache."

The U.S. ban on purchases of medicines, he said, is "one of the most grotesque and miserable things" the United States could do.

The United States has maintained an embargo on trade with Cuba since the early 1960s, although at one point medicines were exchanged for release of Cuban dissidents. The more recent moves toward resumption of diplomatic and trade relations were suspended when Cuba expanded its military activity in Africa.

Castro reiterated that the trade boycott and the U.S. naval base at Guantanamo were "fundamental obstacles" to better relations.

On Nicaragua, Castro predicted that the government of President Anastasio Somoza soon will fall. The people are "rising up because they just can't stand Somoza any longer," he said, dismissing the Nicaraguan strongman's charges that Cuba was arming guerrillas there.

"Somoza is the son of U.S. interventionism," Castro said, referring to the role of the U.S. Marines in bringing Somoza's father to power 46 years ago.

Castro's visit on this Caribbean island appears to have been a success. Mexican President Jose Lopez Portillo, who gave President Carter a stern address during a recent visit to Mexico, described Castro as a "guide" and a "liberator" and the Cuban revolution as small in size but immense in quality."

Castro several times went out of his way to congratulate Mexico on its oil policy, refusing to "consider its energy development as a function of North American oil needs."

Cuba will benefits modestly from Mexico's vast engergy reserves. A well-placed Mexican official said Mexico will ship "liquid propane gas to Havana to make up for the current shortage there."

He said technicians of the state oil monopoly Pemex have been giving maintenance courses and assisting in the repair and modernization of Havana's oil refinery.

The partial shutdown caused by the work created a severe shortage of propane cooking gas for Cuban homes.

"We will send the Cubans the gas to relieve their shortage," the official said. "It's an emergency arrangement, not a long-term deal."

Besides the short-term gas delivery and the assistance in oil technology, the official said there have been "no talks of Mexican oil sales to Cuba that I know of."

Another agreement signed today is for a joint venture in production of machinery to cut sugar cane. Cuba is to supply the technology and Mexico is to build the plant in this country.

Castro and Lopez Portillo held several hours of private talks. Officials said they did not know whether Lopez Portillo tried to persuade Castro to sign the Tlatelolco treaty, a Mexican initiative that bans nuclear weapons from Latin American. On several occasions in the past, Mexicans have asked the Cubans without success.

The Mexican president was accompanied here by his foreign minister of one day, Jorge Castaneda. Lopez Portillo fired Castaneda's predecessor, Santiago Roel, Thursday, along with two other Cabinet members.

Castaneda is a prestigious career diplomat known for his firm nationalist stand in dealing with the United States. But the change, it is widely believed, is the result of Cabinet squabbles and is not designed to bring a change in policy.

In a joint communique before Castro's departure, the two nations announced a number of joint projects in the fields of agriculture, fishing, television and culture. CAPTION: Picture 1, FIDEL CASTRO . . . no aspirin for a toothache; Picture 2, President Fidel Castro talks with reporters at a press conference during his two-day visit to Cozumel, Mexico. AP