OAKLAND — A plainclothes CHP officer drew and pointed his gun at demonstrators Wednesday night after he and his partner were unmasked as police officers, witnesses said, a maneuver that CHP officials defended Thursday as necessary for the officer to defend himself and his partner against an unruly crowd.

The two-minute melee, captured by freelance photographers and shared widely online Thursday, is perhaps the most dramatic scene in weeks of unrest and protests in Oakland, Berkeley and San Francisco. It has also raised questions over how police, particularly the California Highway Patrol, have attempted to control crowds protesting two grand jury decisions to not indict officers who have killed unarmed black men in New York and Ferguson, Missouri.

In response to the confrontation, agency officials on Thursday stood firmly behind the officers and revealed some surveillance tactics, from creating phony Twitter accounts to dressing like demonstrators to gather intel.

“The highway patrol is extremely cognizant and very sensitive to the display of a gun,” Avery Browne, chief of CHP’s Golden Gate Division, said in a telephone news conference. “It’s very disturbing and upsetting to individuals who are attempting to protest, and we recognize that.

“I invite the public to bring me the proof that our personnel conducted themselves in an inappropriate way (so that we can investigate it).”

Carlos Villarreal, executive director of National Lawyers Guild, said the tactics used by CHP echoed Tuesday night’s protest, at which officers fired bean bags from a freeway overpass at protesters below. NLG has had representatives at local protests to document police confrontations to be used in any criminal prosecution of protesters or civil lawsuits.

“It’s pretty troubling especially in light of other things the CHP has done,” Villarreal said. “The CHP has shown that it is acting recklessly.”

Browne said the actions of the two plainclothes officers, who he did not identify, would be investigated but that he believed they acted appropriately and that the officers remained on active duty. Both are detectives in the agency’s Bay Area auto theft unit, he said.

Browne said the officers were present because CHP’s aircraft was unable to follow the crowd due to bad weather. The officers followed the protesters in an unmarked car as they marched from Berkeley into Oakland, listening for information about targeting freeways, and relayed that information to the CHP, allowing uniformed officers to prevent blockages at Highway 24 and Interstate 880, Browne said.

Since the protests began last month, officers have created and used Twitter accounts to monitor protesters, and plainclothes officers have followed crowds on foot, he said.

The officers got out of the car in the area of Franklin and Ninth streets after demonstrators broke windows at a T-Mobile store and damaged a nearby ATM. They followed protesters along Jackson Street, staying at the side of the protest as it moved toward Lake Merritt. By Whole Foods Market near 27th Street, a few of the approximately 150 marchers identified them as possible police officers and began yelling to alert others, Browne said.

As the crowd neared 27th Street, someone approached one of the officers — who was on foot, with a dark handkerchief tied around his face in the style of so-called black bloc protesters — and pulled down the hood on his sweatshirt, then struck him in the head, Browne said. One of the officers attempted to arrest that person, wrestling with him on the ground.

As that officer wrestled with the protester, his partner identified them as police officers, drew his baton and ordered the crowd to stand back, Browne said. As some closed in despite the order, the officer drew his gun. He told his bosses he did so to protect his partner.

Browne said the officer later told him: “Chief, I didn’t know if I was going to make it out of this thing alive.”

Oakland officers came to assist the undercover officers. The protester who had struck one officer was arrested on suspicion of felony assault on a peace officer, Browne said.

One protester, Dylan Leavitt-Phibbs, 23, said Thursday marchers had been questioning the pair for blocks, but they wouldn’t identify themselves as police officers or protesters. Leavitt-Phibbs, who recently moved from Minnesota to Berkeley, said he pulled a mask from one of the officers after he refused to identify himself. Leavitt-Phibbs, who is white, said he was not arrested, but a fellow protester, who is black, was.

“If there was a uniform officer we would act accordingly,” he said. “There was a moment as soon as I pulled off his mask he was still acting like a civilian. Our feeling was betrayal.

“What struck me was how quick they were to pull out guns.”

Witnesses reported to NLG that the officers were inciting the crowd by banging on windows, but when asked if that was the case, Browne said: “Absolutely not.”

CHP officials did not identify the person who was arrested. They said they are looking for a blond woman who allegedly kicked one of the officers in the head.

Browne said that a marcher who was accused of being an undercover police officer Tuesday night had been assaulted by other marchers and had his car damaged. “I think the mob mentality took over,” Browne said. “They used that excuse to effect violence on a human being.”

Still, he said he would continue to assign plainclothes officers to protests.

“An officer in uniform tends to incite the crowd,” Browne said. “They could easily be attacked. When you’re gathering information, it is helpful that you be dressed like the individuals you’re following.”

Browne defended the tactics, saying police had information that demonstrators were armed with bottles, rocks and guns, though no weapons were seen or recovered.

“They were outnumbered. They were assaulted,” Browne said. “At that point, two officers were not going to arrest 30 or 40 individuals.”

Browne said the CHP would continue to use plainclothes officers as needed to protect public safety.

David DeBolt covers breaking news. Follow him at Twitter.com/daviddebolt.