Bill Cosby and one of his attorneys, Angela Agrusa, right, arrive at the courthouse in Pittsburgh for the third day of jury selection. (Gene J. Puskar/AP)

A ferociously fought jury-selection battle marred by racism allegations and the surprise removal of a seated juror ended late Wednesday with the empaneling of seven men and five women who will decide whether comic legend Bill Cosby is guilty of sexual assault.

Many of the themes that will undergird Cosby’s trial once testimony begins June 5 — sex, race and the influence of media coverage — were present over three days of tense questioning of jurors. The 79-year-old comedian, a slender wooden cane often dangling at his side, mostly watched the proceedings unfold with an air of detachment, often staring toward the ceiling with his eyes closed. It wasn’t until late Wednesday when a similarly aged man jauntily complained of gastrointestinal discomfort that Cosby laughed out loud and broke into a wide smile that would have been familiar to millions of fans in his 1980s heyday as television’s biggest star.

The laughter was a rare break from hours of wrangling over the makeup of the panel, with both sides telegraphing their preferences: the defense angling to block young women from being empaneled and the prosecution making moves to stop the seating of some black jurors. Defense attorneys failed to convince Judge Steven T. O’Neill that prosecutors were “systematically excluding” potential black jurors. After the jury was empaneled, lead prosecutor Kevin Steele took a swipe at the defense, calling comments about “the optics” of the case “nonsense.”

In the end two black jurors were selected: a woman who said she knew little about the case and a soft-spoken, extremely polite man who left the courtroom before his selection was announced by cheerily saying, “Have a good day” to the judge. The percentage of African Americans in the final composition of the jury actually slightly exceeds that of Allegheny County, where the pool was drawn — 16 percent of the jury is black while the county is 13 percent black.

Crucially, black jurors were also named as the first and second members of a 6-person group of alternates. In high-profile cases, it’s not uncommon for jurors to be removed for various reasons during the trial. The alternates, who listen to testimony with the main panel, can suddenly be cast into starring roles. There was no better evidence of that fact than the strange sequence of events that took place late Wednesday. After much sniping between attorneys, Judge O’Neill selected an African American man, even saying in chambers, “Congratulations, you’re the 12th juror” to the man in a voice loud enough that could be heard in the courtroom.

(Erin Patrick O'Connor,Manuel Roig-Franzia/The Washington Post)

But moments later, things began to unravel. The judge returned to chambers with the attorneys for a half-hour, then emerged and announced that a juror — a white man who appeared to be in his 50s or 60s and had been seated on Monday, the first day of jury selection — would have to be removed for “deeply personal” reasons that could not be publicly divulged.

The startling removal — upending a carefully and heavily fought-over panel — forced the judge to return to the jury pool to find another juror. He eventually settled on a 30-year-old white woman, who has a 2-year-old child. The woman said her mother could care for the child during the two to three weeks the jury will be sequestered 300 miles away in suburban Philadelphia.

“If I have something that I’m needed to do, I will do it,” the woman said, even after the judge gave her multiple opportunities to beg out of service.

O’Neill, a folksy and animated jurist who spoke intimately with jurors, said the woman’s response was “inspiring.”

Cosby is charged with three counts of aggravated indecent assault for alleged drugging and sexually assaulting Andrea Constand, a woman he’d met while she was an operations manager for the Temple University women’s basketball team. The jury selection for the case was moved to Pittsburgh because of intense pretrial publicity in Montgomery County, Pa., where Cosby has a large home. But the trial will be held before a sequestered jury in Norristown, Pa., a Philadelphia suburb.

The final group of 12 skewed toward young and middle-aged people, with eight who appeared to be in their 20s and 30s, three who seemed to be more middle-aged and one elderly man who entered the courtroom with a cane.

Little information has been made public about the jurors, so their ages and most of their professions are unknown. However, a few of those who were seated revealed details about their lives, including a white schoolteacher and father of a 3½ -year-old who appeared to be in his 20s or early 30s and said he was a regular NPR listener. Another young white man on the jury said that he’d never been in a courtroom before and that considered all the news about Cosby “just a bunch of talk.” At least two jurors said they’ve known someone who was the victim of sexual assault, and one of the alternates said a close family member was “sexually abused.”

The selection process was shaped in part by a stern, gray-haired man who sat in a prime position between Cosby and his two high-powered lead attorneys. The man’s name was never been announced, but his central role in the saga was clear. The attorneys sought his input on almost every major decision. Previously undisclosed court records reveal that the man is David Weinberg, the founder of a Minnesota-based jury consulting firm called JuryScope. He was assisted by three associates: Emily Ryan, Amy Ziegler and Carrie Mason.

On the surface, the firm seems like something of an unconventional choice, since most of the cases it mentions on its website revolve around corporate issues, such as antitrust laws and insurance. But one of the team members — Mason — touts her experience on another case that played out amid a media firestorm. Mason worked on the case of Scott Peterson, a California man convicted of killing his pregnant wife, Laci, in a case that became a tabloid sensation.

When the jury she helped pick this week arrives in suburban Philadelphia for Cosby’s trial, the tabloids will be there, too.