There is almost nothing that Senator John McCain of Arizona talked more about during his 2008 campaign for president than his desire to eliminate budget earmarks.

He called himself “the Sheriff” and usually got laughs when he said his campaign against the budget gimmick meant he didn’t win the “Miss Congeniality” award from his Senate colleagues. He said repeatedly that it was the quintessential difference between himself and then-candidate Barack Obama.

So the irony is especially rich that it is the man who beat him in 2008, along with some of Mr. McCain’s earmark-loving Republican colleagues, who now appear on the verge of a deal to eliminate them.

Mr. Obama embraced earmark reform in his Saturday radio address, prodded by the soon-to-be House Speaker, John Boehner of Ohio. And on Monday, Senator Mitch McConnell of Kentucky, the minority leader, reversed his long-standing defense of earmarks and signed on to an effort to ban them.

“There is simply no doubt that the abuse of this practice has caused Americans to view it as a symbol of the waste and out-of-control spending that every Republican in Washington is determined to fight,” Mr. McConnell said in a statement on Monday.

For Mr. McCain, the prospect of an end to earmarks — funds that lawmakers direct to projects in their own states or districts — is welcomed news. In a Twitter message on Monday, the senator from Arizona wrote that McConnell had made an “important stmt on earmarks, I congratulate him!”

But the moment must be bittersweet for Mr. McCain, who was never quite able to turn the earmark battle cry into a political winner, either on the campaign trail or in the Senate.

Brooke Buchanan, a spokeswoman for Mr. McCain, notes that “it’s not the point to be a ‘political winner.’ It’s meant to be what’s in the best interest of our country and the best use of the taxpayer dollar.”

But over the years, Mr. McCain waged a losing battle to end the practice. Over time, he transformed his longstanding opposition to earmarks into a broader indictment against the entire political and legislative system in Washington.

On the airborne Straight Talk Express in the summer of 2008, Mr. McCain sought to link earmarks to corruption in Washington and to both jailed super-lobbyist Jack Abramoff and his adversary, Mr. Obama.

“The fact is that it corrupted the system,” Mr. McCain said of earmarks. “So it’s the difference between the reformers and the go-along-to-get-along system. Clearly, Senator Obama has never taken on his party. He’s gone along with the system of earmarks and pork barrel projects. That’s what he’s done. I haven’t. And I haven’t been popular with my party. That’s a big, big difference.”

But Mr. Obama repeatedly batted back the argument by noting that all the earmarks in the world don’t add up to a large percentage of the federal budget. In their final debate at Hofstra University in New York, Mr. Obama said that “earmarks account for one half of one percent of the federal budget.” He added, “It’s not going to solve the problem.”

For Mr. McCain, the issue became a highly personal one. People who questioned his commitment to earmarks were often accused of questioning his personal integrity.

On a campaign trip to Allentown, Pennsylvania to highlight a hospital that used technology to keep down health care costs, reporters pointed out to Mr. McCain that the technology improvements were largely paid for by legislative earmarks.

In a session at the back of his bus that day, he got very angry at the reporters, telling them that one or two good earmarked projects did not justify the system of earmarks. “The mafia does good,” he said, using an analogy that made its way into several stories the next day. “There are some projects that the mafia are engaged in that do good.”

Asked if he could offer a list of earmarked projects that he would not spend money on, he lashed out again.

“Of course not. Of course not. Why should I?” he barked, adding that reforming the overall system was the point. Pressed for a list, Mr. McCain said, “How could I possibly? Do you want me to go through 9,000 projects? Are you crazy?”

But in the end, the political pressures created by the 2010 midterm elections, and the rise of the Tea Party movement may have done what Mr. McCain never could. As Tea Party-backed lawmakers began arriving in the capital this week, it became clear that their opposition to earmarks would become an early, symbolic victory.

In a statement Monday evening, Mr. Obama said he welcomed Mr. McConnell’s support of earmark reform and noted that he had supported limitations on earmarks and increased transparency for those who requested them.

“But we can’t stop with earmarks as they represent only part of the problem,” Mr. Obama said. “In the days and weeks to come, I look forward to working with Democrats and Republicans to not only end earmark spending, but to find other ways to bring down our deficits for our children.”