A sex scandal in Detroit has forced out its chief of police, the second person in two years to resign from the position after allegations of inappropriate relations with a female officer in the department.

At a news conference Monday, Mayor Dave Bing announced that the chief, Ralph L. Godbee Jr., had decided to step down after news emerged last week that he was having an affair with an internal affairs officer in the department.

“He was very contrite yesterday,” said the mayor, who spoke with Godbee on Sunday. “He felt he had let me down. He felt he had let the department down. And he felt he had let the people of Detroit down.”

Bing added, “It bothers me a great deal that it has come to this.”

Reports that Godbee, 44, would resign had been circulating for nearly a week after the officer he had an affair with, Angelica Robinson, posted a picture of herself on a social media website holding a department-issued handgun to her mouth, apparently after she discovered that Godbee was in San Diego with another woman.

“Apparently she was contemplating making the ultimate sacrifice out of humiliation, frustration and pain over what it was the chief was putting her through,” said her attorney, David A. Robinson (who is not related to Angelica Robinson), adding that the department had taken her gun away and ordered that she get a mental evaluation.

In 2010, Godbee replaced Warren C. Evans, who was also accused of having an affair with a subordinate officer. At the time, Bing did not specifically cite the relationship as the reason for Evans’ resignation but said that “certain decisions” had compromised the chief’s ability to lead the department.

Soon after, it became known that Godbee had also had an affair with the same female officer as Evans had. Bing said Monday that he had known about that relationship but still appointed Godbee because he believed he was the right person for the position.

“He confided in me that he had changed his ways,” Bing told reporters Monday. “I had no reason not to believe that.”

News of Godbee’s resignation comes about a month into the corruption trial of Kwame M. Kilpatrick, the gregarious former mayor of Detroit who resigned in 2008 after lying under oath about a relationship with his chief of staff.

Detroit currently has no policy against fraternization among city workers, Bing said, although he now intends to institute one.

“I retire proud of the fact that I was able to assist during a time when our city has most been in need,” Godbee wrote in a letter released by the department.

Chester L. Logan, a 38-year veteran of the Detroit Police Department who has been in charge of day-to-day operations for two years, will take the reins while the search for a new chief takes place, the mayor said.

Detroit continues to be plagued by high crime rates and financial turmoil that has challenged its police force. As of Saturday, the most recent police department data available, there have been 298 killings since January, an increase of more than 25 percent from the same time period in 2010.

“We’re going to get through this,” Logan said at his first public appearance as interim chief. “Collectively, we’re going to get through this.”