It took only three remarkable shots  one each by snipers firing from a distance at dusk, using night-vision scopes, the officials said. Within minutes, rescuers slid down ropes from the Bainbridge, climbed aboard the lifeboat and found the three pirates dead. They then untied Captain Phillips, ending the contretemps at sea that had riveted much of the world’s attention. A fourth pirate had surrendered earlier.

Image Richard Phillips, right, the captain of the U.S.-flagged cargo ship Maersk Alabama, after his rescue on Sunday with Cmdr. Frank Castellano, the commanding officer of the Navy destroyer Bainbridge. Credit... U.S. Navy, via Reuters

Shortly after his rescue, Captain Phillips was taken aboard the Bainbridge, underwent a medical exam and was found to be in relatively good condition for a 53-year-old seafarer who had been held since Wednesday by pirates who had demanded $2 million for his life. He called home and was flown to the Boxer, an amphibious assault ship also off the Somali coast. Arrangements were being made Sunday night for his return home to Vermont.

“I share the country’s admiration for the bravery of Captain Phillips and his selfless concern for his crew,” Mr. Obama said in a statement. “His courage is a model for all Americans.”

Jubilation over the dramatic rescue reached from the White House to Underhill, Vt., Captain Phillips’s hometown, and from personnel aboard the Bainbridge to the cheering, fist-pumping 19-member crew of the captain’s cargo ship, the Maersk Alabama, docked in Mombasa, Kenya.

Captain Phillips, who was said to be resting comfortably, spoke to officials of the Maersk Line, who quoted him as saying: “The real heroes are the Navy, the Seals, those who have brought me home.” He also spoke to his wife, Andrea, and two college-aged children in Underhill, where dozens of yellow ribbons fluttered on the white picket fence of his home and two small American flags jutted up from the lawn.