In 1977, he ran in a Democratic primary for mayor against six candidates, including the incumbent, Abraham Beame, and a rising Queens political star named Mario Cuomo. Nobody expected the nebbish from the liberal clubs of Greenwich Village to emerge ahead of the pack, but Koch squeaked by with promises of pragmatic solutions to save a bankrupt city.

As mayor, he persuaded Congress to give New York additional federal loan guarantees that gave him time to put the city's fiscal house in order. He balanced the budget, cut spending, negotiated new municipal labor contracts and seeded programs to rebuild neighborhoods destroyed by years of neglect and arson.

In 1981, with endorsements from both the Democratic and Republican parties, he was returned to office with 75% of the vote.

Koch's ubiquity became such a source of tension among City Hall journalists not wanting to miss a story that his press secretary eventually installed a buzzer in the room where they were stationed so she could warn them simultaneously if the mayor was on the loose.

His critics and friends agreed that one of Koch's biggest mistakes was to fall for his own media image. Such hubris led him to run for governor in 1982, wasting a year on what he later admitted was a foolish effort. Again he was up against Cuomo in a primary, but this time Koch lost.

After losing, he wrote "Mayor" (1984), a bestselling, tell-all memoir that took harsh shots at rivals and friends. In 1985, he was reelected mayor for the final time.

That third term was quickly swamped by corruption scandals and investigations involving Koch's political allies that sent some of his aides and cronies to jail. While he was never personally implicated in wrongdoing, his administration was tainted at a time when it was also facing monumental challenges associated with crack use, AIDS and homelessness.

When he sought a fourth term, he was defeated for the Democratic nomination by Manhattan Borough President David N. Dinkins, who became the city's first African American mayor.

Jonathan Soffer, a historian and author of the 2010 book "Ed Koch and the Rebuilding of New York City," said Koch's loose lips continually caused trouble. During the 1988 presidential primary, he said Jews and other supporters of Israel would be "crazy" to vote for the Rev. Jesse Jackson.

"Koch tried to say he treated everyone equally and was racially neutral," Soffer said. "But his rhetoric was perceived by some in the black community as racially biased."

When asked later how he felt when people expressed regret at his loss, he offered a classic Kochian meld of charm and chutzpah. "The people have spoken," he said, "and the people must be punished!"

In the end, Koch never stepped far from his old City Hall office.

"In many ways Ed Koch never stopped being mayor," Police Commissioner Raymond Kelly said Friday in a statement, noting that Koch constantly gave the Police Department his opinion. "I was privileged to consider him a friend, and I am grateful that I had a few more times to be with him, on Tuesday and again last night, before he finally left New York for someplace better — although he'd probably argue that's not possible."

tina.susman@latimes.com

Susman is a Times staff writer. Baum is a former Times staff writer.

Times staff writer Elaine Woo contributed to this report.