The Democrat also will pay the city $1 million in restitution, lose his license to practice law, and 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 a trove 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.
Later Thursday, he said public servants should be held accountable to the people they serve.
"I take full responsibility for my own actions and for the poor judgment that they reflected," he said on live television. "I wish with all my heart that we could turn back the hands of time and tell that young man to make better choices, but I can't."
But Kilpatrick left the door open to a return to public life.
"I want to tell you, Detroit, that you have set me up for a comeback," he said. "I truly know who I am. I truly know where I come from. In Detroit I know who I am. And I know because of that, there's another day for me."
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 the Democrats in Michigan, a crucial battleground state in the presidential election.
"This gives us hope. He's not a king," said Monica Smith, 24, of Detroit, a college student who was on the courthouse steps. "This is a huge victory for the city of Detroit. He was not a role model. He was a thug. I'm definitely optimistic."
For the scandal's nearly eight months, Kilpatrick repeatedly said the nation's 11th-largest city hadn't missed a beat. He liked to say trash was still being picked up, snow was cleared, parks were open and the grass was cut.
But Detroit - which has struggled for decades with high crime, unemployment and a shrinking population - has been hurt mightily by the mortgage crisis and the downturn in the auto industry, faces a possible budget deficit of $65 million, and was recently declared one of America's "fastest-dying" cities by Forbes magazine.
Some business leaders said the city suffered while the criminal case hung over the mayor.
"I'm not sure there were lost projects. Things were put on hold," said Doug Rothwell, head of Detroit Renaissance, an economic development group. "For business people, if you don't know the environment, you don't invest. What you found was everything was put on pause. Hopefully we can get things off the pause button."
Richard E. Blouse, president of the Detroit Regional Chamber, said: "A lot of damage has been done to the region's image."
The scandal stems from a whistleblower 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 Sept. 11.
Kilpatrick's lead attorney, Dan Webb, said the mayor didn't make up his mind until Thursday morning and that Kilpatrick believed "the deck was stacked against him" for many reasons, including intense media coverage, state and local politics, the difficulty of picking a fair jury and the mayor's own dishonesty.
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.
Associated Press writers David Runk in Detroit and Kathy Barks Hoffman in Lansing, Mich., contributed to this report.