PARIS, JULY 17 -- Soviet President Mikhail Gorbachev's sudden decision to remove the final obstacle to German unification followed months of intense and often unpredictable diplomacy and was motivated in the end by Gorbachev's need to devote full attention to his sinking economy, according to senior U.S. officials familiar with the events.

When Gorbachev told West German Chancellor Helmut Kohl on Monday that he would not object to membership of a unified Germany in the Western military alliance, no one was more surprised than Secretary of State James A. Baker III, who learned of it from news reports. Although Baker said he felt Gorbachev would take this position eventually, U.S. officials were saying as recently as last week that it would not happen at least until September because of Gorbachev's domestic troubles.

West German officials said today that they, too, were taken aback by the abrupt Gorbachev decision, which they had been pursuing for months but did not expect so soon.

A senior State Department official said the Gorbachev shift appeared to be, in part, a political calculation that he had triumphed over his conservative critics at the 28th Communist Party Congress last week and should move while he is riding high. "He's been somewhat freed" from political restraints, the official said.

Gorbachev "wants to get on with it in terms of inside," the official added, referring to another push for Soviet economic reforms expected in the fall. "There's a sense of urgency on his part. He knows he's got to move, and move quickly."

At the same time, the Bush administration had made German unification -- and winning Gorbachev's consent for it -- one of its highest foreign-policy priorities. But the goal had proved frustratingly elusive. One senior State Department official described Gorbachev as an unpredictable running back, zig-zagging across the field to confuse his pursuers.

West Germany and the United States waged an intensive seven-month diplomatic effort to coax Gorbachev to the West's position, an effort fraught with disappointments and frustrations, according to U.S. officials.

At the previous round of the so-called Two-plus-Four talks in East Berlin, for example, Soviet Foreign Minister Eduard Shevardnadze appeared to dig in his heels on unification, offering a proposal unacceptable to the other participants. U.S. officials concluded that Shevardnadze was forced to make the proposal to deflect hard-line criticism of Gorbachev at the party congress then about to begin.

Likewise, although Gorbachev had made an enticing comment at the Washington summit about Germany's freedom to choose its alliance, U.S. officials did not take it to be genuine acceptance of a united Germany in the Western alliance.

Then they worried that Gorbachev had been so distracted by the party congress that he would not be prepared to deal with Germany until fall.

Recently, however, some pieces started to fall into place. West Germany moved quickly with loans and credits for Moscow and was pressing its allies to do so, as well. At a foreign ministers' meeting in Copenhagen recently, Baker broached to Shevardnadze specific ideas for expanding the role of the 35-nation Conference on Security and Cooperation in Europe, and the Soviet minister was surprisingly enthusiastic. One U.S. official said this was the first time that he got "a very different sense" that Moscow would be ready to deal.

At the East Berlin meeting, Shevardnadze said five times that the Soviet leadership would watch the outcome of the London summit, a participant said. Taking a cue from the issues that the Soviet minister had said he was most concerned about, such as the role of the CSCE and a nonaggression statement, the U.S. and its allies wrote into the London declaration what they believed the Soviets wanted to hear. And, finally, Kohl delivered to Gorbachev what an aide called "the bottom line," his commitment to limit the size of the German army.