DETROIT — Mayor Kwame Kilpatrick was bounced from office Thursday in a deal with prosecutors that will send him to jail and put an end to the sex scandal that embarrassed this chronically struggling city and preoccupied its government for months.

The 38-year-old "Hip-Hop Mayor," who brought energy and excitement to City Hall when he took office in 2002, pleaded guilty to obstruction of justice and will get four months behind bars.

The Democrat will also pay a $1 million fine and lose his license to practice law, and he cannot run for any elected office for five years. His resignation will take effect in two weeks.

Kilpatrick was charged earlier this year with perjury and other offenses for denying he and his chief of staff had an affair. The scandal broke wide open in January with the release of lusty text messages between the two of them that appeared to contradict the mayor.

"I lied under oath," the beefy former college lineman said in court Thursday. His wife, Carlita, watched from the second row, occasionally closing her eyes.

Coming after eight months of turmoil and demands that Kilpatrick step down, the plea bargain was met with relief from politicians and ordinary Detroit residents alike. His departure could also remove a major embarrassment for Barack Obama and Democrats in Michigan, a crucial battleground state in the presidential election.

The scandal stems from a whistle-blower lawsuit filed by two former police officers who accused Kilpatrick of retaliating against them for trying to investigate misconduct by the mayor and his security detail. Questioned under oath in 2004 and 2007, Kilpatrick repeatedly denied having an affair with his chief of staff, Christine Beatty.

But the Detroit Free Press later obtained text messages between the two — some of them sexually explicit — and published excerpts. Kilpatrick and Beatty were later charged.

In addition to perjury, Kilpatrick was accused of misleading City Council when he secured its approval of an $8.4 million settlement with three former police officers. Prosecutors said he settled to keep the text messages from becoming public.

Beatty is expected to enter a plea bargain during her next court appearance, on Thursday.

On Thursday, Kilpatrick also pleaded no contest to assault, for allegedly shoving a detective who was trying to serve a subpoena in the text-message case. His sentence in that case will be served at the same time as the one for obstruction.

Kilpatrick's sentence will be officially imposed on Oct. 28, and he will immediately report to jail, Wayne County Prosecutor Kym Worthy said.

Under the city charter, any mayor guilty of a felony is automatically expelled from office.

In court on Thursday, Circuit Judge David Groner asked Kilpatrick if he understood he was giving up the right to be innocent until proven guilty.

"I gave that up a long time ago," Kilpatrick replied.

The plea bargain came just one day after Democratic Gov. Jennifer Granholm convened an extraordinary hearing on whether to oust Kilpatrick as mayor. The City Council called on Kilpatrick months ago to resign but had no authority to remove him.

Granholm described Thursday's events as "a sad but historic story" and said they "serve as a profound reminder to us all, public officials and citizens, that a public office is entrusted to the person who holds that office but belongs to the people who are served by that office."

City Council President Ken Cockrel Jr. will succeed Kilpatrick as mayor until a special election is held.

Obama, who has avoided Kilpatrick in campaign trips to Michigan and discouraged him from attending the Democratic convention, welcomed the change in leadership.

"Sen. Obama believes that the serious charges against the mayor were a distraction the city could not afford and that his immediate resignation is the only way for the city to move forward and get back to business," spokesman Brent Colburn said.

The son of a Detroit congresswoman, Rep. Carolyn Cheeks Kilpatrick, Kilpatrick was 31 when he was elected in 2001, becoming the youngest mayor in Detroit history. His youth, energy and diamond stud earring endeared Kilpatrick to many fellow blacks, especially young ones.

But Kilpatrick's first term was tumultuous. He came under fire for racking up thousands of dollars in travel on his city-issued credit card and leasing a luxury Lincoln Navigator for his wife.

Under his leadership, though, Detroit landed baseball's 2005 All-Star Game and the 2006 Super Bowl. And Kilpatrick's ability to work with business leaders also has been credited with an overhaul of the city's riverfront and development downtown.