WASHINGTON — The United States on Saturday transferred nine Yemeni detainees from its wartime prison at Guantánamo Bay to Saudi Arabia, completing a long-sought diplomatic deal ahead of a planned visit to Riyadh by President Obama in the coming week.

The effort to persuade the Saudi government to take the prisoners began in the Bush administration and finally resulted in an agreement in February. Current and former officials familiar with the negotiations called the timing of the transfer, which reduced the population at Guantánamo to 80 prisoners, a coincidence.

“There have been a lot of discussions with the Saudis over the last few years, and they have been emphatic that it was very important to close Guantánamo,” said Cliff Sloan, who served as the State Department envoy for negotiating detainee transfers in 2013 and 2014. “They wanted to help with that. But the one thing they weren’t willing to do for a long time was actually accept Yemenis. That’s why this is a major breakthrough.”

The military brought each of the prisoners from the Afghanistan War to the American naval base at Guantánamo about 14 years ago, soon after President George W. Bush’s administration opened the prison in early 2002. Later, the Bush administration decided to try to close it, a goal the Obama administration has shared.