The Cuban foreign minister ended a surprise visit to Pakistan yesterday after offering President Fidel Castro's mediation in the Afghan crisis in an apparently Soviet-backed move to help ease nonaligned and Moslem criticism of the Kremlin.

Details of Castro's offer were not disclosed. But analysts here suggested that the Cuban leader, acting on behalf of the Kremlin, sought to mollify Pakistani President Mohammed Zia ul-Haq, who played a key role when an Islamic foreign ministers' conference in January condemned the Soviet invasion and proclaimed an effective boycott of the Soviet-backed Afghan government.

U.S. officials said the mission of Foreign Minister Isidoro Malmierca was designed to salvage some of Cuba's standing in the nonaligned movement. Cuba was among nine non-aligned nations that approved the Afghan invasion in a vote at the United Nations. The current chairman of the 85-nation nonaligned bloc is Castro.

Before visiting Pakistan, Malmierca visited the Soviet Union and Afghanistan, presumably to coordinate Castro's diplomatic approach.

A Pakistani spokesman said Malmierca, who carried Castro's message to Zia, conferred with the Pakistani president and his top foreign policy adviser Agha Shahi during his two-day visit to Islamabad.

Analysts here said the Cuban move appeared to be aimed primarily at encouraging a dialogue between Afghan leader Babrak Karmal and Zia before the next conference of Moslem foreign ministers that is to be held in Pakistan next month.

These analysts said that Moscow and Havana may be seeking to exploit Pakistan's "wavering" following Zia's rejection of an American offer of $400 million in economic and military assistance extended after the invasion of Afghanistan.

The Pakistanis initially maintained that the U.S. aid package was insufficient and sought more money.Later they insisted that it would not be prudent for Pakistan to depend for its security "on any single power" and stressed instead Pakistan's nonaligned credentials.

Pakistani Foreign Ministry officials said Castro's message to Zia outlined the Cuban leader's desire to "serve the cause of peace and contribute toward a political solution of the problems which have arisen" following the invasion of Afghanistan.

Following Malmierca's departure, Shahi told reporters that his government's position "is based on the resolution adopted" at the January meeting of Moslem foreign ministers.

Apart from unanimously demanding an immediate withdrawal of Soviet troops, that conference also suspended Afghanistan's membership in the Moslem group, leaving it virtually isolated except for the Soviet Bloc countries.

There was no information about the contents of Zia's reply to Castro, which was handed to Malmierca before his departure. But the talks between the Cuban official and Zia were described as useful.

Since the soviet invasion, Moscow's relations with Pakistan have deteriorated rapidly and the Soviets have accused Zia's government of helping Afghan rebels in collusion with the United States and China.

A rebel organization in Pakistan yesterday claimed that Afghan insurgents have raided a government jail in the northeastern province of Kunduz and freed about 1,200 prisoners.

The Islamic Party's statement said the raid was organized because several prominent rebel leaders in the jail had been scheduled for execution.