In the end, it took late-night comedians, a less genteel legislature and a backtracking governor to change the course of Virginia’s highly contentious antiabortion bill.

The measure, which would require women to get an ultrasound before having an abortion, was headed for the law books late last week. But over the course of nine days, it became clear that the legislation’s sponsors didn’t realize one key fact — that for an ultrasound to determine the age of a fetus, as mandated by the bill, it would usually require a vaginal probe.

That detail helped the issue catch fire on cable TV and inspired protests on Richmond’s stately Capitol grounds. By Wednesday, Gov. Robert F. McDonnell had withdrawn his support for the original legislation and left in question the fate of a substitute.

McDonnell, a potential Republican vice presidential candidate who a week ago was stumping for former Massachusetts governor Mitt Romney in Michigan, found himself in an unflattering national spotlight. It was a spectacular and unexpected reversal for McDonnell and other Virginia Republicans, who in November’s elections grabbed hold of every lever of power in Richmond.

But for a governor with national political aspirations, the controversy exploded just as his efforts got underway to help deliver a swing state for his party in this fall’s presidential and U.S. Senate contests.

Asked about what specifically led to his reversal, McDonnell and his office declined to comment. But legislators and staff members, who spoke on the condition of anonymity to discuss the matter freely, said the governor began having doubts about the legislation last weekend.

The unraveling begins

The slow-motion unraveling of the original ultrasound bill began weeks ago, when one Democratic senator buttonholed another after the health committee voted to approve it. Sen. George L. Barker (D-Fairfax), who has a background in health care and opposed the measure, knew that in the earliest stages of pregnancy, only a vaginal ultrasound can detect the fetus.

Barker pulled aside Sen. Ralph S. Northam (D-Norfolk), a doctor who had served as technical adviser on the bill. Barker asked: Since 90 percent of abortions take place in the first trimester, wouldn’t the bill mostly require a more invasive ultrasound, than the ordinary “jelly-on-the-belly” variety. The bill didn’t say.

Northam said he didn’t know, and Barker suggested he check with medical experts.

“I said, ‘You didn’t say anything about this,’ ” Barker said of the committee meeting.

Northam consulted with medical experts, who confirmed that a vaginal probe would be needed in the early stages of pregnancy.

So when the measure came up for a vote in the Senate on Jan. 31, Barker and Northam raised the issue during the floor debate — but delicately. They used the words “transvaginal” and “internal.” But they didn’t use startling terms such as “vaginal penetration” and “state-sponsored rape,” which eventually came to dominate the debate.

Mindful of the teenage Senate pages sitting in the chamber, Barker said, they wanted to be sensitive with their language.

Barker was sure that the senators understood what he was saying. Sen. Janet D. Howell (D-Fairfax) certainly got the point. In an effort to highlight what she considered a gross personal intrusion, she proposed an amendment requiring that men get a rectal exam.

“Prior to prescribing medication for erectile dysfunction, a physician shall perform a digital rectal examination and a cardiac stress test,” declared the amendment, which the Senate clerk read aloud on the floor.

The amendment failed, and the ultrasound billed passed. McDonnell, in a break from his usual practice, announced that day that he would sign the legislation.

Media enter the picture

But seeds of the bill’s undoing were sprouting.

Howell’s floor amendment caught the attention of the national media, including MSNBC’s Rachel Maddow, who ran a segment soon afterward. But Maddow did not make it clear that the ultrasound was anything other than the external variety performed later in pregnancy.

On the Senate floor, Howell had not been explicit about the intrusive nature of the ultrasound. “I thought I was being brave by saying ‘digital rectal,’ ” she said.

But Howell came to understand that some of her Senate colleagues failed to grasp how much the type of ultrasound in question was like the probing she’d proposed for men.

“I don’t think they understood what kind of ultrasound they were talking about,” she said. “I think they thought it was a mini-massage and not something approaching rape. People are squeamish about using words like ‘vagina,’ but in this case, it was necessary for people to understand how invasive this bill is.”

Even people leading the fight against the ultrasound bill were in the dark. Tarina Keene, executive director of NARAL Pro-Choice Virginia, said she did not realize the ultrasound would have to be the vaginal variety until a colleague from out of state pointed that out to her in the first week of the session.

“It wasn’t my original talking point,’’ she said.

By Feb. 10, when a House committee took up the bill, Keen was passing out pictures of vaginal ultrasound probes and using blunt language. By the time the bill came to the House for a vote, a few days later, Del. David L. Englin (D-Alexandria) was using such terms as “vaginal probe.” Other Democrats compared the procedure to rape.

The measure passed the House anyway. But more commentators and comedians picked up on the invasiveness of the test.

Maddow returned to the topic with a segment that showed images of the probe emblazoned with slogans such as, “If you can read this, your government is too close.” In a jab at McDonnell, another probe was captioned, “I can see the White House from here.”

Saturday Night Live” piled on last weekend with a sketch that played on the word “transvaginal.” (“It’s my favorite airline!” comedian Amy Poehler declared.)

McDonnell asks for advice

Earlier Saturday, McDonnell, a social and fiscal conservative who has governed as a moderate, began calling legislators to ask for advice. He also consulted other Republican governors, advocates and lobbyists. He met with staff members and legislators several times.

On Monday, more than 1,000 protesters demonstrated on Capitol Square, wearing T-shirts inscribed with messages that included “Virginia is for lovers, not probers.” On Tuesday night, the governor’s senior staff and House Republican leaders met in his office to write an amendment amid growing controversy.

If they flipped on the TV to relax later Tuesday night, they would have seen Jon Stewart add his withering two cents’ worth on “The Daily Show.”

Mincing no words about the invasive nature of the ultrasound, Stewart ran audio, video and newspaper clips of ultrasound bill supporters speaking out against other forms of what they perceived to be government intrusion. McDonnell was heard calling full-body pat-downs at airports “probably over the line with regard to people’s concerns about privacy and their civil liberties.”

On Wednesday morning, women’s rights advocates delivered petitions with more than 33,000 signatures protesting the ultrasound and the “personhood” bill, which has since died and would have defined a fertilized egg as a person.

By that afternoon, McDonnell had issued a statement asking lawmakers to change the bill so that it would require women to undergo an ultrasound but would not mandate the vaginal type.

“No person should be directed to undergo an invasive procedure by the state, without their consent, as a precondition to another medical procedure,” he said.

Bob Holsworth, a commentator and former political science professor at Virginia Commonwealth University, said similar laws in other states have not caused firestorms like the one in Virginia.

“Over time, the opponents of the bill learned how to oppose it,” he said.

Holsworth found himself saying things in a TV interview on the legislation that he never expected to say in public. “Never in my life,” he said, “could I imagine that I would be talking about transvaginal ultrasounds.”