Senator John McCain was one of the earliest advocates of American military action against the Islamic State in Iraq and Syria. So it has been vexing for Mr. McCain to be battling persistent — and false — Internet rumors that he not only helped invent the group but also knows its leader, Abu Bakr al-Baghdadi, the self-proclaimed caliph of the Muslim world and America’s latest Public Enemy No. 1.

The rumors are based partly on images of a Syrian fighter who resembles Mr. Baghdadi, seen in photographs with Mr. McCain — some originally posted on Twitter by the senator — during his visit in May 2013 to northern Syria. He met members of the Free Syrian Army, an insurgent group that opposes ISIS and that President Obama, in a speech Wednesday on his new strategy for battling ISIS, has vowed to strengthen.

Nurtured by conspiracy blogposts, social media and photo-altering tricks, the false rumors of Mr. McCain’s relationship with ISIS have taken on a life of their own. One doctored photo shows the senator, an Arizona Republican, pinning a medal on Mr. Baghdadi’s chest.

Last month the rumors received new vitality when a left-leaning American veterans group asserted that the senator had posed for photographs “with ISIS militants.” The rumors were further bolstered with the news that an American recruit to ISIS shared the senator’s surname. And on Wednesday in Iran, where many people already believe ISIS is an American plot to destabilize their country, the state television asserted that Senator McCain was an ISIS cohort. As proof, it showed one of the photographs.

Brian Rogers, Mr. McCain’s communications director, was not amused. The man who was misidentified as Mr. Baghdadi in the photographs, Mr. Rogers said Thursday, was a commander of the Northern Storm Brigade, a group linked to the Free Syrian Army. Mr. Rogers declined to identify him by name, saying he feared for the man’s safety.

Mouaz Moustafa, the executive director of the Syrian Emergency Task Force, a Washington-based group that helped to arrange the senator’s visit, said all of the people who appeared in those photographs were at risk. When ISIS published the first issue of its magazine, Mr. Moustafa said, it featured one of the photographs and proclaimed, “We have to cut all these people’s heads off.”

On Aug. 19, the same day that ISIS released an Internet video depicting the beheading of James Foley, an American journalist, the group VoteVets.orgasserted that Mr. McCain’s demands for strong action against ISIS was hypocritical. In a fund-raising email, the VoteVets chairman, Jon Soltz, anIraq war veteran, wrote that while Mr. McCain had visited Syria, “he paused for some photos — including some with ISIS militants.” The senator’s office reacted angrily, calling the assertion an “obviously false smear.”

Mr. Soltz could not be reached on Thursday for comment, but a representative of the group responded by referring to Mr. Soltz’s emailed statement at the time to The Arizona Republic: “When we say he took photos with ISIS fighters, we mean that he took photos with representatives of forces that he supported, many of which later became ISIS. It’s frightening to think what ISIS would be today, if John McCain had his way, and if we had sent even more weaponry their way.”

Mr. McCain’s efforts to blunt the impact of the photographs took another unforeseen twist a few weeks later, when American intelligence officials confirmed that a 33-year-old Minnesota man named Douglas McAuthur McCain, an ISIS recruit, had been killed by a rival Syrian insurgent group. To the rumor-driven Arab media, the coincidence of the surname was a smoking gun.

“The stuff was already irrationality in motion,” said Mr. Moustafa. “Then a guy actually named McCain joins ISIS. It added fuel to the fire.”