MOSCOW, SEPT. 12 -- The four victorious World War II powers signed a treaty today terminating their rights and responsibilities over Germany and restoring its sovereignty, lost with the defeat of the Nazis in 1945.

Calling the treaty signing a "rendezvous with history," Secretary of State James A. Baker III said that "it represents the end of a 45-year journey."

The formal termination of Four Power rights in Germany will become effective only when all the nations have ratified the treaty. However, the foreign ministers decided to "suspend" the rights Oct. 1 -- two days before Germany becomes unified through the merging of East into West Germany.

The treaty allows U.S., British and French troops to remain stationed in Berlin "for the duration of the presence" of Soviet troops in what is now East Germany. The Soviet Union has agreed to withdraw all of its estimated 370,000 troops in East Germany by 1994.

After its wartime defeat, the allies divided Germany and its capital into four occupation sectors, which became East and West Germany, and East and West Berlin -- as well as the front line of the East-West confrontation that marked the Cold War. The Four Powers have since maintained certain of their occupation rights, such as stationing troops in their sectors and controlling air corridors in Berlin.

The foreign ministers of Britain, the United States, France, the Soviet Union and West Germany, and the prime minister of East Germany, came to Moscow in part to pay tribute to Soviet President Mikhail Gorbachev, whose "new thinking" allowed the Communist governments of the Soviet-controlled East Bloc to collapse over the past year and who also gave Soviet approval to the German desire to unify.

Gorbachev watched today as Hans-Dietrich Genscher of West Germany signed the six-page document first, followed by East Germany's Lothar de Maiziere, Roland Dumas of France, Eduard Shevardnadze of the Soviet Union, Baker and Britain's Douglas Hurd.

Genscher said the treaty signing was "a day of joy and jubilation" for all Germans but that "in this hour we remember the victims of war and totalitarian domination. . . . We would not want that agony to be repeated."

Then, borrowing a sentence from the treaty, he said, "Only peace will emanate from German soil."

The White House hailed the treaty signing as a "historic event." Spokesman Marlin Fitzwater said, "In the glare of today's Persian Gulf crisis, it is easy to forget the anxiety and turmoil for East and West {Germany} during the unfolding of these events, but they were successfully managed, and today we can look forward to a unified Germany that is a partner with its neighbors, and committed to fostering a new world order."

Today's signing ceremony, held in the luxurious October Hotel, marked the end of the so-called Two-Plus-Four talks started earlier this year to cope with the accelerating pace of German unification. Much of the diplomacy has been directed at assuaging concerns by the Soviet Union and other nations about a resurgent German state.

The turning point in the diplomatic efforts came when Gorbachev told West German Chancellor Helmut Kohl July 15 that the Soviet Union had decided to drop its objections to a unified Germany's joining the North Atlantic Treaty Organization. Since then, government officials have been trying to put the finishing touches on a document codifying the new Germany's external relations.

Today's planned signing ceremony almost came unglued at the last moment. Experts had put months of work into refining compromise language on such issues as the Polish border and Germany's military status. However, on Tuesday night, as the foreign ministers arrived in Moscow, there was disagreement over what kinds of military maneuvers NATO troops could conduct in what is now East Germany.

A round of hurried meetings ensued. Genscher raced over to the hotel where Baker was sleeping and woke him up, and they talked until 1:30 a.m. This morning, Baker met with Hurd, Genscher and Dumas. Some Soviet officials wondered loud if they should postpone the signing ceremony. Baker and Genscher then went to see Shevardnadze, and they agreed to a one-paragraph attachment to the treaty, U.S. officials said.

German and Soviet officials also are expected to sign other agreements here this week setting down details of the Gorbachev-Kohl talks in July, including economic assistance. In addition, the treaty is to be accompanied by a separate letter from Germany promising that Soviet war memorials and graves will be protected, and that the Nazi party will be banned.

For months, the United States had envisioned a "final settlement" agreement to end Four Power rights over Germany, not a treaty. This was partly out of deference to Germany, which wanted to avoid signing a "peace treaty" and partly because the United States felt that submitting a formal treaty for Senate ratification would be time-consuming. But in the last few days, other nations said they wanted the document to be a treaty, and the United States gave in.

State Department officials said they have begun talks with the Senate leadership on U.S. ratification of the treaty.

The treaty text describes a complex transition in which military activities will be limited on the territory of what is now East Germany, a sensitive issue for the Soviet Union.

While Soviet troops are still present in the east or are in the process of pulling out, the only other forces permitted there will be German "territorial defense units" not integrated into NATO. Other countries are barred from stationing troops or carrying out military activities there during this period.

Once Soviet forces have left, some German troops affiliated with NATO can move into the east, but without "nuclear weapons carriers." Some conventional weapons that have a "dual use" capability -- such as 105-mm artillery pieces that can fire nuclear-armed shells -- will be permitted as long as they are equipped only for conventional fighting.

Finally, the document says foreign armed forces and nuclear weapons "will not be stationed in that part of Germany or deployed there." However, the definition of the word "deployed" was left to Germany itself "in a reasonable and responsible way," taking into account the other nations' security. Officials said this point might allow maneuvers in the former East Germany by certain NATO-affiliated troops.

White House spokesman Fitzwater said unspecified scheduling conflicts prevented President Bush from accepting an invitation from Kohl to attend unification ceremonies Oct. 3 in Berlin.