Mehdi Karroubi, a reformist candidate in last June's disputed presidential election, was visiting Qazvin to attend a mourning ceremony for opposition protesters when he was attacked, the website said.
Karroubi was apparently uninjured in the attack.
"Around 500 basiji [pro-government militia] and residents of nearby villages surrounded the place where he was and attacked the building with stones, breaking windows," Sahamnews.org said on Friday.
After four hours, anti-riot police reportedly intervened and got Karroubi to his car but it was shot at as it left the venue.
"As his car was pulling away, it was attacked and hit by gunfire. But, as it is an armoured car, only the windows were damaged."
The attackers chanted slogans in support of the government and Ayatollah Ali Khamenei, the supreme leader, the website said.
"Our town is not a place for hypocrites," they were report as saying.
The Rajanews.com website, which is close to the government, confirmed that Karroubi's motorcade had come under attack by a stone-throwing crowd, but it made no mention of gunfire or basij militia being involved.
"Several thousand angry people surrounded the house where Mehdi Karroubi was," it said.
"Anti-riot police tried several times to clear a way through the crowd to extricate Karroubi's vehicle which finally left under a hail of stones, eggs and tomatoes."
Foad Izadi, a professor of political communication at Tehran University, told Al Jazeera: "The political situation in Iran is not very calm.
"When you have one of the leading opposition members visiting a town that is somewhat more religious, it is understandable some people are not going to like it."
Along with Mir Hossein Mousavi, another reformist leader, Karroubi complained that the re-election of Mahmoud Ahmadinejad, the president, was marred by vote rigging and fraud.
The accusation sparked a series of protests in the capital Tehran and elsewhere, which later spilled over into violent clashes between opposition supporters and security forces.
Hundreds of opposition leaders, activists and protesters have been arrested since a crackdown on the protests shortly after the election.