Photo by Gage Skidmore and licensed under CC BY-SA 4.0

McCain, who had been battling brain cancer, was known for his heroism in Vietnam, his independent-mindedness and recently his rebukes of Trump.

John McCain, prisoner of war, presidential candidate and one of the most influential American politicians of his generation, has died after suffering from brain cancer.

The six-term senator from Arizona, who was 81, had been absent from Washington since last December but remained outspoken to the end, railing against Donald Trump and urging defense of the post-war liberal democratic order.

A statement from his office said: “Senator John Sidney McCain III died at 4.28pm on 25 August 2018. With the senator when he passed were his wife Cindy and their family. At his death, he had served the United States of America faithfully for 60 years.”

Barack Obama, who defeated McCain for the presidency in 2008, said he and his rival had shared “a fidelity to … the ideals for which generations of Americans and immigrants alike have fought, marched, and sacrificed.”

Obama added: “Few of us have been tested the way John once was, or required to show the kind of courage that he did. But all of us can aspire to the courage to put the greater good above our own. At John’s best, he showed us what that means. And for that, we are all in his debt.”

McCain’s death will leave a void in a Republican party increasingly turning away from bipartisanship at home and engagement overseas in favour of the president’s brand of divisiveness and isolationism.

Trump, who clashed bitterly with McCain and in recent weeks had refused to mention him by bame, tweeted a brief statement on Saturday. He did not praise the man. “My deepest sympathies and respect go out to the family of Senator John McCain,” the president wrote. “Our hearts and prayers are with you!”

McCain’s legacy will be keenly debated. No stranger to controversy, he was a staunch supporter of President George W Bush’s invasion of Iraq in 2003 and his choice of Sarah Palin as running mate in his unsuccessful bid for the White House in 2008 is now seen as a harbinger of the tide of rightwing populism he came to resist.

McCain’s first brush with death came in July 1967 when, as a a navy pilot in the Vietnam war, a missile exploded near his fuel tanks, causing a ship fire that killed 134 troops. Three months later he was shot down over Vietnam, breaking both arms and a leg, and was captured by the North Vietnamese.

McCain was offered early release because his father was a four-star admiral but rejected it until his fellow PoWs were also freed. During five years in captivity, including more than two in solitary confinement, he suffered beatings and torture almost daily.

That McCain displayed rare fortitude in the “Hanoi Hilton” was common cause – until Trump mocked him during the 2016 election campaign, telling a conference in Iowa: “He’s not a war hero.” The future president, who avoided the draft five times, added: “He was a war hero because he was captured. I like people that weren’t captured.”

McCain retired as a captain in 1981 after earning a number of decorations and ran for Congress in Arizona the following year. He switched from the House to the Senate in 1987 and forged a reputation as a military hawk – he was one of the Iraq war’s biggest champions – and maverick willing to defy the right and work across the aisle on issues such as campaign finance and immigration reform.

In 2000 he finished second to George W Bush in the Republican presidential primary after an ugly campaign. Eight years later, he beat Mitt Romney to the nomination, only to come up against a political force of nature: Obama.

McCain’s military pedigree and independent streak won respect but his choice of the unqualified Palin as running mate – apparently designed to appeal to Christian evangelicals who deserted him in 2000 – damaged his credibility. He was swept away by a wave of optimism that carried Obama into the Oval Office but delivered a memorably gracious concession speech.

He declined to support the 2016 Republican nominee after the release of an Access Hollywood tape in which Trump boasted about groping women’s genitals. “When Mr Trump attacks women and demeans the women in our nation and in our society, that is a point where I just have to part company,” McCain said.

What might have been a gentle drift into retirement became instead a final, furious last stand as the elder statesman saw his beloved party shifting from under him. As others fell into line, McCain became one of Trump’s chief Republican critics, jabbing at what he perceived as the president’s weak response Russian aggression and self-defeating hostility to Muslims.

In February last year he travelled to Munich to reassure anxious allies: “Make no mistake, my friends: These are dangerous times, but you should not count America out, and we should not count each other out.”

In October, receiving the Liberty Medal in Philadelphia, he issued what many saw as a repudiation of Trump’s “America first” ethos: “To fear the world we have organized and led for three-quarters of a century, to abandon the ideals we have advanced around the globe, to refuse the obligations of international leadership and our duty to remain ‘the last best hope of earth’ for the sake of some half-baked, spurious nationalism cooked up by people who would rather find scapegoats than solve problems is as unpatriotic as an attachment to any other tired dogma of the past that Americans consigned to the ash heap of history.”

By then McCain had undergone surgery to remove a blood clot from near his left eye and was found to have an aggressive form of brain tumor known as glioblastoma. When the diagnosis was announced, Obama commented: “Cancer doesn’t know what it’s up against.”

McCain was no liberal but he opposed both Trump’s first attempt at a travel ban for six majority-Muslim nations, calling it potentially “a self-inflicted wound in the fight against terrorism”, and the confirmation of Gina Haspel as director of the CIA because of her refusal to fully disown torture.

And on the grounds that the legislation was being rushed through Congress, he was also central to the failure of two attempts to repeal Obama’s signature healthcare legislation. In response, Trump continued to use the bully pulpit berate the senator, though not by name.

McCain spent his final months with his second wife, Cindy, at their ranch in Sedona, Arizona. He continued to issue statements and tweets, published a valedictory book and took part in an HBO documentary that some likened to starring in his own obituary.

“I’ve been tested on a number of occasions,” he told the film makers. “I haven’t always done the right thing. The important thing is not to look back and figure out all the things I should have done, and there’s lots of those, but to look back with gratitude.”

Former President Barack Obama, who defeated McCain in the 2008 election, issued a statement Saturday night praising McCain for viewing public service as a “patriotic obligation:”

“John McCain and I were members of different generations, came from completely different backgrounds, and competed at the highest level of politics. But we shared, for all our differences, a fidelity to something higher-the ideals for which generations of Americans and immigrants alike have fought, marched, and sacrificed. We saw our political battles, even, as a privilege, something noble, an opportunity to serve as stewards of those high ideals at home, and to advance them around the world. We saw this country as a place where anything is possible – and citizenship as our patriotic obligation to ensure it forever remains that way.

“Few of us have been tested the way John once was, or required to show the kind of courage that he did. But all of us can aspire to the courage to put the greater good above our own. At John’s best, he showed us what that means. And for that, we are all in his debt. Michelle and I send our heartfelt condolences to Cindy and their family.”

David Smith and Paul Owen

Help deliver the independent journalism that the world needs, make a contribution of support to The Guardian.