Former president Richard Nison, forced out of office five years ago, spent most of this morning behind a wall of secrecy and bodyguards at the sumptuous residence of his old ally, the shah of Iran, who was deposed last winter.

On his way here, in Mexico City, Nixon held a news conference and praised Shah Mohammad Reza Pahlavi as a friend of 25 years and an ally of the United States for 30.

He also said the U.S. choice in Nicaragua is "not between President [Anastasio] Somoza and somebody better, but between President Somoza and somebody much worse."

Then, in an early-morning caravan of eight limousines, he was driven 45 miles south to Cuernavaca.

The monarch's security guards, a force of about 60 Mexicans, Iranians and Americans, kept journalists well away from the shah's compound in Cuernavaca, a mountain resort town 50 miles south of Mexico City favored by wealthy retired Americans for its strong sun and year-round spring weather.

Security was tight, not only because of Nixon's status as a former president but also because the Islamic regime that ousted the monarchy in February has imposed a death sentence on the shah and offered a reward to anyone who carries it out.

Before Nixon left the villa, the shah, Empress Farah and their 18-year-old son, Reza Cyrus, allowed reporters and photographers onto a patio at one of the houses in the complex for a three-minute photo session.

"How did you enjoy your visit?" one journalist asked Nixon.

"Very much," he replied.

As the Nixon caravan drove away, one of the cars smashed into the rear of another, causing serious damage to both cars. Nixon was not in either car and there were no injuries.

Earlier Nixon told the Mexico City news conference that he came to assure the shah "That not only I but millions of Americans in the U.S. are still his friends" and that the two men would talk over "world affairs, especially the Middle East.

"One principle . . . that I have followed . . . is that whether it's in Vietnam or it's in Iran, you don't grease the skids for your friends," Nixon said.

"If the United States does not stand by its own friends we are going to end up with no friends."

Nixon also said he deplored the situation in Nicaragua, where the Carter adminstration has called on a long-time ally, Somoza, to resign because his army has been unable to quell the rebellion by leftist-led guerrillas seeking the end of the 42-year-old Somoza family dictatorship.

Nixon said he has received indications that Somoza "is prepared to resign."

Nixon drove 60 miles Thursday night from his home at San Clemente, Calif., to Tijuana, just across the Mexican border, and took a late-night schedulaed DC10 flight of Aeromexico, a Mexican airline. His party included 14 Secret Service agents assigned to his protection.

During the flight, Nixon emerged from the first-class compartment, drawing cheers from the 256 passengers, most of them Mexican.

A few chanted "Nixon, Nixon" and "We want Nixon" in English. CAPTION: Picture 1, RICHARD NIXON . . . urges U.S. to stand by "friends"; Picture 2, Former president Nixon, left, and the deposed shah pose for photographers in Mexico. AP