Mayor Bob Filner agreed Friday to resign in return for the city’s help defending him against claims he groped, kissed and made lewd comments to women, allegations that shook and embarrassed the city and turned the former 10-term congressman into a national punch line.

Immediately after Filner told the City Council he would leave office next week, the California attorney general’s office opened a criminal investigation into the allegations. Attorney general spokesman Nicholas Pacilio confirmed that a criminal investigation is underway but declined to elaborate.

Filner was regretful and defiant during a City Council meeting as he explained the “the toughest decision of my life.” He apologized to his accusers but insisted he was innocent of sexual harassment and said he was the victim of a “lynch mob.”

“The city should not have to go through this, and my own personal failures were responsible and I apologize to the city,” Filner said after the council voted 7-0 on a deal that ended a political stalemate after 17 women publicly accused him of harassment.

“To all the women that I’ve offended, I had no intention to be offensive, to violate any physical or emotional space,” he said. “I was trying to establish personal relationships but the combination of awkwardness and hubris led to behavior that I think many found offensive.”

The city will pay Filner’s legal fees in a joint defense of a lawsuit filed by the mayor’s former communications director and pay for any settlement costs assessed against the mayor except for punitive damages, said City Attorney Jan Goldsmith. The city would also pay up to $98,000 if Filner wants to hire his own attorney.

Goldsmith said the city was obligated to provide his legal defense no matter what.

Filner, backed by a sometimes boisterous crowd of supporters, challenged the City Council to pursue a laundry list of his policy initiatives, ranging from addressing climate change to bringing the Olympic Games to the region. He warned of dire consequences if his priorities are ignored by well-entrenched power brokers.

“I am responsible for providing the ammunition,” he said. “I did that and I take full responsibility, but there are well-organized interests who have run this city for 50 years who pointed the gun, and the media and their political agents pulled the trigger.”

Filner choked up as he apologized to his former fiancee, Bronwyn Ingram, who ended the relationship just before the scandal erupted and called for him to resign.

“I love you very much. You came to love San Diego as much as I did and you did memorable things in the short time that you were first lady,” he said.

Filner, 70, a liberal Democrat, served 20 years in Congress before becoming mayor of the nation’s eighth-largest city.

Some of Filner’s closest political allies and all nine members of the council called on him to quit.

On Friday, just before the council vote, the Democratic National Committee took the extraordinary step of passing a resolution demanding Filner leave.

Dozens of people spoke for and against the mayor before the council convened behind closed doors to discuss terms negotiated between Filner and the city attorney.

“Without the mayor’s resignation, our city will continue to be paralyzed by this scandal, progress will be arrested and our focus will continue to be monopolized by this dark chapter in our history,” said Laura Fink, a political consultant who accused Filner of patting her buttocks in 2005 when she was deputy campaign manager to the then-congressman.

Still, many who came to the special meeting supported the embattled mayor, hailing his work on behalf of civil rights and struggling minority groups.

“When my children ask me, ‘Where were you when the public lynching of Mayor Filner took place?’ I will tell them I was not an accessory,” said Enrique Morones, president of immigrant advocacy group Border Angels.