Former “comfort women” Gil Won-ok, left, and Lee Yong-soo, who were forced into wartime sexual slavery for Japanese soldiers, sit during an anti-Japanese rally in Seoul on Dec. 30. (Jung Yeon-Je/AFP/Getty Images)

The day after North Korea’s nuclear weapons test this week, President Obama called Japanese Prime Minister Shinzo Abe and South Korean President Park Geun-hye to confer about this latest crisis in the region. But before ending each call, Obama made a point of congratulating the leaders on resolving their dispute over Japan’s use of wartime sex slaves.

The president praised them for “having the courage and vision to forge a lasting settlement to this difficult issue,” but the gesture was not just a casual courtesy. It reflected a deep U.S. investment in a diplomatic deal it worked to cultivate — and is working to safeguard — as it comes under fire in both countries.

Deputy national security adviser Ben Rhodes described the Dec. 28 accord as “a long-standing source of tension between two key U.S. allies in the Asia-Pacific.”

According to Rhodes, Obama raised the issue in nearly every meeting “he’s had with the leaders of Japan and South Korea over the last several years.” Repairing the Japan-South Korea relationship was essential to Obama for two reasons. A closer alliance between the two could help counterbalance China’s growing military and economic influence in the region, and help keep North Korean aggression in check.

“This wasn’t just a question of wanting our two friends to get along; it mattered strategically,” Deputy Secretary of State Antony J. Blinken said in an interview.

A statue of a teenage girl symbolizing “comfort women” who served as sex slaves for Japanese soldiers during World War II, is seen in front of the Japanese Embassy in Seoul on Wednesday. (Jung Yeon-Je/AFP/Getty Images)

The United States did not broker the accord or attempt to perform the type of role it has played in the Mideast peace process or some other foreign disputes. But Obama administration officials — including the president himself — intervened at pivotal points over the past two years to help bring Abe and Park closer together, fostering an environment that made it possible for the two countries to settle their grievances over the “comfort women.” And it is one of the few remaining spots in foreign policy where congressional Republicans offered full support for the administrations’s goal, bolstering its case.

“The U.S. factor was very large, because in both bilateral relationships, this was a problem,” said Michael J. Green, senior vice president for Asia at the Center for Strategic and International Studies.

Interviews with more than a half-dozen top officials from all three nations depict a process where the White House decided to press for a resolution of the comfort-women issue even as it sought to minimize responsibility should the reconciliation effort fall apart. Abe and Park each had their own powerful political incentives to strike the deal, which ultimately led them to negotiate a solution so delicate that part of the compromise stipulates that the two sides will stop criticizing each other in the international community.

Under the deal, the Japanese government agreed to put $8.3 million into a South Korean fund to support the 46 surviving comfort women and to help restore their “honor and dignity” and repair their “psychological wounds.”

The South Korean government agreed to explore whether the civic group that placed a bronze statue of a comfort woman that sits outside the Japanese Embassy in Seoul would remove it.

While women and girls from other nations — including China, the Philippines and other Southeast Asian nations — were also forced into servitude by the Imperial Japanese Army before and during the war, the issue is especially sensitive in South Korea. By the spring of 2014, Abe and Park had yet to meet despite the fact that they had been in office at the same time for more than a year.

Students hold portraits of deceased former South Korean “comfort women” during a weekly anti-Japan rally in front of the Japanese Embassy in Seoul on Dec. 30. (Kim Hong-Ji/Reuters)

In many ways, the most important U.S. contribution to the process came at the start, when Obama brought the two leaders together during the nuclear security summit meeting at the residence of the U.S. ambassador to the Hague in March 2014. As Abe entered the room, he addressed Park in Korean. She smiled.

While the meeting was focused on North Korea’s nuclear program, not disputes stemming from World War II, several aides said it helped open up a line of communication between Abe and Park, and underscored the idea of what was at stake.

“Figuratively and politically, it created a platform for them to focus on what united them, and not divided them,” said Assistant Secretary of State for East Asian and Pacific Affairs Daniel R. Russel, who attended the meeting.

At that point, according to Cho Hyun-dong, deputy chief of mission at South Korea’s embassy in Washington, there was enough “positive momentum” to start bilateral negotiations on the question of the comfort women.

The talks intensified in 2015, a year that marked the 70th anniversary of the end of World War II and the 50th anniversary of the normalization of diplomatic ties between South Korea and Japan. Cho said the milestones marked an “auspicious” opportunity, adding, “There was a sense of urgency, because the average age of the victims is about 90.”

As a staunch conservative who was eager to propel Japan into a more prominent international role, Abe had his own incentives for striking a deal.

Still, at times even issues not directly related to the negotiations threatened to derail them. In June, a push by Japan to win UNESCO recognition for a handful of industrialized sites angered South Korean officials, who felt the Japanese had not adequately described the use of forced laborers in the factories during wartime.

When both sides contacted the United States, according to a senior administration official who spoke on the condition of anonymity to discuss the talks, it reminded them of the need to focus on “the 21st century, rather than the 20th century.” The Japanese and South Koreans agreed on how to describe the forced labor, and the talks on the comfort women resumed.

U.S. Ambassador to South Korea Mark Lippert said that throughout the talks, the administration sought to remain impartial even as it encouraged both countries to reach a settlement. Just as Lippert and U.S. Ambassador to Japan Caroline Kennedy worked with officials in their respective countries, Secretary of State John F. Kerry spoke privately with both country’s leaders, hosting a dinner for Abe this spring in his home on Boston’s Beacon Hill.

“There was listening to both sides — that style of finding the middle was important,” he said in an interview. “It wasn’t a pressure situation. It was, ‘You’re doing the right thing here, and if you could get there, that would be terrific.’ ”

A handful of key Republicans — including Sens. John McCain (Ariz.) and Cory Gardner (Colo.), and Rep. Matt Salmon (Ariz.) — made the same case to the two governments as the president.

Still, the deal has come under criticism from factions on the right in Japan and on the left in South Korea, as well as from the comfort women themselves.

Yoo Hee-nam, 86, said in an email that she and other victims should have been allowed to participate in the talks, and that the matter is not a political dispute that needs to be resolved, “but the issue of humanity and reclaim[ing part] of it that was destroyed a long time ago.”

While Yoo said Japan did not concede “enough,” conservatives in Japan argue their government gave in too much. Abe faces “fierce criticism domestically” for making the deal, his special adviser Katsuyuki Kawai said in an interview Friday in Washington.

Kawai said that, given the fragile state of the pact, “It’s very important that all relevant, involved parties should nurture the spirit of the agreement,” and that the United States can play a role in ensuring that.

Cho was just as adamant about Japan’s obligations, saying it had finally “acknowledged responsibility for the comfort women, with no qualifiers. Now, the agreement must be implemented faithfully without backtracking from its unequivocal language.”

But even with the pushback against the deal, Kawai said, the comfort-women agreement has already begun to pay dividends. Abe and Park spoke within 24 hours of North Korea’s nuclear test, which Kawai said showed working through the thorny issue of the women’s wartime claims allowed them to confer “through a relationship of trust.”

Given Kim Jong Un’s penchant for conducting nuclear tests without warning, Kawai quipped, “It was right in time for that.”