Shane Bauer and Josh Fattal, the American hikers jailed in Iran on spying charges since 2009, were freed from prison Wednesday and flown to Oman for a joyful reunion with family members.

“We’re so happy we are free,” Fattal told reporters at Muscat International Airport in Oman.

The 29-year-old Californians and a companion, Sarah Shourd, were arrested in the mountainous region straddling Iraq and Iran for supposedly crossing the border; their detention further frayed diplomatic relations between the United States and Iran.

Shourd, who was freed last year on medical grounds and is engaged to Bauer, was on hand in Oman to greet the two men as they ran down the stairs from the private plane that picked them up in Iran.

Oman’s government paid bail of $500,000 for each man, the same sum it paid for Shourd’s release.

The mission to free Bauer and Fattal also included a year-long effort by a group of Washington-based religious leaders and a former U.S. diplomat, all with prior experience dealing with clerics and officials in Iran.

The release came a day before Iranian President Mahmoud Ahmadinejad was scheduled to address the U.N. General Assembly in New York, and it seemed timed at least in part as a goodwill gesture ahead of the speech.

“We are thrilled,” President Obama, also in New York at the United Nations, told reporters after being informed of the release. “They shouldn’t have been held in the first place.”

In a statement issued by the White House, Obama praised “the tireless advocacy” of the families. He also expressed gratitude to Oman’s Sultan Qaboos bin Said, the ruler of the tiny monarchy on the Arabian Peninsula, as well as to Iraqi President Jalal Talabani and the Swiss government.

Last week, Ahmadinejad announced that Bauer and Fattal would be released “in a couple of days.” But a day later, Iran’s judiciary, a bastion of Shiite Muslim clerics, issued a statement stressing that only it has the right to pardon prisoners. It effectively embarrassed the Iranian president, in another signal that the clerics’ once-firm support for him has waned considerably.

Bauer and Fattal were convicted of espionage charges in a closed trial last month. They denied the charges and maintained that if they crossed the Iranian border, they did so accidentally while hiking with Shourd.

Before Ahmadinejad’s pardoning hand was checked, the campaign to free the men had gained momentum thanks to a meeting in a New York hotel conference room in September 2010, shortly after Shourd’s release. There, a private delegation met with Ahmadinejad and Iran’s U.N. ambassador, Mohammad Khazaee.

Pressing for the men’s release were Cardinal Theodore E. McCarrick, archbishop emeritus of the Archdiocese of Washington; the Right Rev. John Bryson Chane, an Episcopal bishop and interim dean of Washington National Cathedral; and William Miller, a former U.S. ambassador who helped bring home many Americans during the 1979 Tehran hostage crisis.

“We said, ‘We hope the two boys would be released,’ and all he said was, ‘You should come to Tehran,’ ” McCarrick recalled. No date was set for the trip.

Chane and McCarrick had visited Iran before and had communicated with the country’s supreme leader, Ayatollah Ali Khamenei.

Just days before their departure last week, Chane went to the Cosmos Club in downtown Washington to meet with the financial backer of his trip: Search for Common Ground, a 29-year-old nonprofit group that promotes peace in conflict zones and gained notoriety in the late 1990s for taking American wrestlers to Iran for exhibition matches.

John Marks, the group’s president, said in an interview that he handed over two envelopes, each containing $5,000, for Chane and McCarrick. Marks also provided Turkish Air tickets for the two clerics — and for Bauer and Fattal.

“We had a very strong assurance — not 100 percent — that the delegation would bring the hikers home,” Marks said. “That was the reason, frankly, we went ahead and bought this tickets. This was the real deal. They wouldn’t have gone for nothing. . . . We put our money on the line.”

Another member of the delegation was Nihad Awad, executive director of the Washington-based Council on American-Islamic Relations. The group tried to meet with Khamenei but never got an appointment; they did, however, get to see Ahmadinejad.

“We said their families have suffered a great deal, that the men had been in prison for too long,” Chane recalled. “And his response was: ‘Not to worry. It will happen within a few days at best.’ Then, he said, ‘Your presence and your work here has been an important part of the release of these two men. You need to understand that.’ ”

Shapira contributed from Washington.