TOKYO — Under intense pressure from the United States, South Korea reversed itself at the last minute Friday and extended an intelligence-sharing pact with Japan, a sign that the Seoul government wanted to halt fraying relations with the two countries.

The decision to remain in the intelligence pact, which South Korea had vowed to abandon just three months ago, appeared to be at least a short-term victory for Trump administration officials. They pressed to preserve the pact, and have been angered over an impasse with South Korea in a separate dispute over the costs of maintaining American troops in the country as a deterrent to North Korea.

South Korea’s decision also came as a bitter trade dispute with Japan showed signs of easing. Taken together, both developments suggested that President Moon Jae-in of South Korea was looking for ways to de-escalate underlying historical tensions, which have caused one of the most serious rifts between the two countries since they normalized relations 54 years ago.

The moves came on the eve of a Group of 20 meeting of foreign ministers in Nagoya, Japan, appearing to set the tone for improvement in relations with the United States and Japan that have been increasingly strained.