Everyone knew it was a disaster. After Kathleen Sebelius appeared on "The Daily Show with Jon Stewart" last October, she and her staff at the Department of Health and Human Services felt she had been sandbagged by Mr. Stewart. At the White House, President Obama's top aides were aghast at her wooden performance.
The White House frustration with Ms. Sebelius crystallized by Thanksgiving, as it became clear in Washington that she would eventually have to go. Republicans were brutalizing her at congressional hearings. The health care website's problems were consuming the White House. Under mounting pressure from congressional Democrats panicking about the fallout from the health care debacle on their fall campaigns, Mr. Obama had already brought in Jeffrey D. Zients, a management guru, to take control of the crisis from Ms. Sebelius.
In the halls of the Hubert H. Humphrey Building, the hulking structure at the foot of Capitol Hill that houses the sprawling social services agency, it was obvious to members of Ms. Sebelius' staff that the president's inner circle was losing confidence in her, several said Friday as the president announced his intention to replace Ms. Sebelius with Sylvia Mathews Burwell, his budget chief.
But three things put off Ms. Sebelius' departure: Mr. Obama's fear that letting people go in the middle of a crisis would delay fixing the website; his belief that ceremonial firings are public concessions to his enemies; and the admiration and personal loyalty that Mr. Obama still felt for Ms. Sebelius and her advocacy for his chief domestic legacy.
Over the next four months, Ms. Sebelius engaged in a kind of slow-motion resignation, largely staying out of the national limelight but crisscrossing the country in a furious effort to enroll people in health insurance and taking comfort from strangers who recognized — and offered thanks — for her efforts.
As the website improved and enrollment numbers neared the administration's goal of seven million people, she began plotting her exit.
"My balance has always been, When? How do you make that decision? I have had conversations off and on with the president about that very thing," Ms. Sebelius said in an interview on Thursday. "It became more definite in early March, when I felt confident that we were well on our way to a robust enrollment."
By then, the conversations between Ms. Sebelius and the president about her future had evolved, she said. In the self-sacrificial and expected conduct of high-level officials under siege in Washington, Ms. Sebelius offered to resign during a series of conversations with Mr. Obama.
And in the way that often happens in Washington, Mr. Obama did not protest.
The result was an orchestrated going-away ceremony on Friday morning that capitalized on the enrollment success and sought to artfully minimize the political damage wrought by the law. Ms. Sebelius entered the Rose Garden by Mr. Obama's side and received an extended standing ovation from White House and Department of Health and Human Services staff members seated on the lawn.
"Yes, we lost the first quarter of open enrollment period with the problems with HealthCare.gov — and they were problems," Mr. Obama conceded. "But under Kathleen's leadership, her team at H.H.S. turned the corner, got it fixed, got the job done, and the final score speaks for itself: There are 7.5 million people across the country that have the security of health insurance, most of them for the very first time."
White House officials — who in private have often complained about Ms. Sebelius' management failings and lack of political antennas — emphasized the positive on Friday, saying that no one had done more to make the long-sought hope of a national health care overhaul a reality. Denis R. McDonough, the White House chief of staff, said that the president and Ms. Sebelius remained close despite — or maybe because of — the political mess they had endured together.
"There's no question that it's a relationship kind of forged in the heat of the campaign and in the heat of that battle with Congress to get the law enacted," Mr. McDonough said. "Out of that came, I think, an enduring friendship and obviously a great amount of respect and trust."
In the interview on Thursday, Ms. Sebelius said it had never occurred to her to step down at the beginning of the president's second term, as so many cabinet secretaries do. And resigning in the middle of the HealthCare.gov disaster would have fed right into her enemies, who wanted her fired to prove their point, she said.
Ms. Sebelius' older brother, Donald D. Gilligan, said he learned of her resignation on Thursday night by email, when Ms. Sebelius wrote that news of it was about to go public.
"I think she's tired," Mr. Gilligan said. He said his sister and the president seemed "quite close," and he rejected the idea that she had been treated unfairly by the White House. "No, not in the slightest. She indicated just the opposite. There was a lot of support and a lot of encouragement to get the problem solved. That was the emphasis."
The day before Friday's announcement, Ms. Sebelius seemed upbeat and relieved. She said she looked forward to spending more time in Kansas, where her husband is a federal magistrate judge, and where her younger son is scheduled to marry at the end of May.
Thursday night, even as the news was breaking, Ms. Sebelius unwound with Valerie Jarrett, a senior adviser and confidante to the president, over dinner at a see-and-be-seen spot for official Washington, the Blue Duck Tavern in the West End neighborhood. Predictably their date was noted in the next morning's news reports, serving to blunt talk that Ms. Sebelius had been forced out.
"No pity party for her," Ms. Jarrett said in an interview on Friday. "She was in a great mood."
The two women swapped 2008 campaign recollections and admired photos of Ms. Sebelius' grandson while they ate. Ms. Jarrett urged Ms. Sebelius to watch a video of Mr. Obama's remarks this week in Texas, where he recalled the similar hostility that once existed with Medicare. She also told Ms. Sebelius how the president met privately on Thursday with people who had written him letters of gratitude for the Affordable Care Act, and how one woman told him, "You saved my son's life."
"Kathleen shares the president's ability to take the long view," Ms. Jarrett said. "I think she takes great comfort in knowing that one year, two years, five years, 50 years from now, no one is going to remember a glitch in the website."
In the interview on Thursday, Ms. Sebelius recalled the dark moments after the appearance on Mr. Stewart's show, when she and her staff were "feeling a little glum about that particular encounter."
At the Hilton hotel lobby in New York where she was staying, a man approached after recognizing her. His daughter died a few years ago, he told her, and if he had not had insurance, the medical crisis would have bankrupted his family.
"You cannot give up on this," she recalled his saying. The two held hands as her aides stood by.
It is moments like that, Ms. Sebelius said as she prepared for her final days as secretary, that leave her with satisfaction in spite of the political fallout from the Affordable Care Act, for her and the president.
"I was never terribly optimistic that this would be easy or not fraught with contentiousness," she said. But, she added: "It was absolutely the right thing to do."