In the week-long diplomatic struggle over the fate of Andrei Berezhkov, 16, the Soviet diplomat's son, the Reagan administration faced the risk of duplicating, in reverse, the case of six Soviet Pentecostalists who took refuge for nearly five years in the U.S. Embassy in Moscow.

A repetition of that stalemate was avoided by a compromise between American and Soviet officials that saved face on both sides, enabling each nation to say its principles had been upheld.

For the Soviet Union, it was maintaining the position that no "official interview" of the youth by American authorities would be permitted. For the United States, it was demonstrating, by having reporters question young Berezhkov instead, that he was "voluntarily departing the U.S."

If he had declared his desire to remain in this country when he was questioned by reporters at Dulles International Airport before his departure Thursday night, that would have assured "a real rough time" for relations between the two nations, a senior U.S. official acknowledged yesterday.

"But there was never the least thought given" to anything but honoring what the boy wanted to do, "whatever the consequences for our bilateral relations," the official said. "I don't think that would have had the least impact on our decision . . . if he had said he wanted to stay."

The complexities of the case behind the compromise that emerged were discussed by officials in the aftermath of what was described officially on the American side as "this painful situation." Officials involved in it expressed considerable satisfaction with the outcome.

With U.S. agents posted around the residential compound for Soviet Embassy employes at Tunlaw road in Northwest Washington, a protracted standoff could have duplicated the lengthy stay of the Soviet fundamentalist Christian family that took refuge in the basement of the U.S. Embassy in Moscow. They left in April, when they were permitted to emigrate from the Soviet Union.

What was left dangling in the resolution of the Berezhkov case was whether the boy in fact had written two letters, one to President Reagan and one to The New York Times, saying that he wanted to stay in the United States.

By denying on Thursday that he wrote them, he supported the official Soviet contention that the letters were forgeries. The Reagan administration, in turn, continues to assert that the letters appear to be authentic, but does not intend to pursue the dispute.

Referring to the letter addressed to Reagan, White House spokesman Larry Speakes said in California yesterday, "We have no reason to doubt its authenticity." Asked how the administration could be sure, Speakes said, "We have our ways."

Asked if young Berezhkov was lying when he denied writing the letters, Speakes said, "I wouldn't put it in those terms."

Administration officials said privately that from the outset they knew they were on tenuous legal grounds in pressing what became an international incident that could have prolonged repercussions.

Because the teen-ager was covered by his father's diplomatic immunity, they said, the only ground they could invoke for holding him in this country was the right of political asylum. That could only be invoked, however, they said, if he claimed it in person.

At noon yesterday, State Department spokesman Alan Romberg said that "the case is closed" by a solution that "met the principles of both sides." He added, "We don't believe it will affect U.S.-Soviet relations. It certainly will not affect our policies toward the Soviet Union."

But within the hour the case was reopened by a new, caustic Soviet protest charging the United States with "disgraceful and inhuman" actions in its handling of the diplomatic struggle. That in turn brought an American statement of "regret that the Soviet authorities have chosen to engage in polemics" by "absurd charges" that the State Department labeled not "worthy of comment."

It was disclosed yesterday that, in addition to the questioning of young Berezhkov by American reporters at the Soviet compound and later at Dulles, Assistant Secretary of State Richard R. Burt spent 45 minutes with the Berezhkov family at the airport.

Romberg said Burt chatted with the boy and his parents, and "everything he saw convinced him that the right judgment had been made." That discussion, Romberg said, was not construed as a "formal interview."

American sources said that, in earlier meetings between Burt and Oleg M. Sokolov of the Soviet Embassy, the Soviets initially suggested a news conference, on embassy grounds, as a way out of the standoff. That solution was rejected as inadequate, the sources said, but out of it came the proposal for a second opportunity for the media to question the youth on American ground in Dulles.

There are many unanswered questions in the strained episode, including what happened during the approximately 14 hours young Berezhkov was missing last week with his father's car, taken without permission. According to sources who know the family, the youth can be described as perhaps as rebellious as many American teen-agers are, often troubling his parents with mischievousness.