In a televised speech on Saturday, he said he suspected there were covert plans to target his life, but he did not elaborate.
Hariri, who made the statement during a visit to Saudi Arabia, said Iran planted "disorder and destruction" in the country and meddled in the internal issues of Lebanon as well as other Arab countries.
Referring to Hezbollah, which is backed by Iran, Hariri said, "Iran's arm ... has managed to impose a fait accompli on Lebanon through the power of its weapons" in the last few decades.
"They have built a state within a state," said Hariri from the Saudi capital, Riyadh.
"I say to Iran and its allies - you have lost in your efforts to meddle in the affairs of the Arab world," he continued, adding that the region "will rise again and the hands that you have wickedly extended into it will be cut off."
Hariri, a leading Sunni politician, has been in office for less than a year, but previously served as prime minister between 2009 and 2011.
He assumed office as prime minister again in December 2016 in a power-sharing government headed by President Michel Aoun, a supporter of Hezbollah, whose members have been charged by the International Court of Justice with assassinating Hariri's father, Rafik, in a 2005 bombing.
The country spent two years in political deadlock without a president before Aoun's election in October 2016, after Hariri endorsed the latter, a move seen by some analysts as a sign of Iran's influence in Lebanon.
His resignation now casts doubt on Lebanon's political future.
Walid Jumblatt, leader of Lebanon's Druze and the Progressive Socialist Party, said Hariri's resignation could adversely affect the country.
He said it was the latest evidence of a power struggle between Saudi Arabia and Iran and urged intensification of diplomatic efforts to defuse tensions.
"Lebanon is too small and vulnerable to bear the economic and political burden that comes with this resignation," Jumblatt said via social media. "I will continue to call for dialogue between Saudi Arabia and Iran."
Iran's foreign ministry said Hariri's resignation would lead to tension in Lebanon and the region.
In a statement published on the ministry's website, Bahram Qassemi, a spokesman, said: "The resigning Lebanese prime minister's repetition of the unrealistic and unfounded accusations of the Zionists, Saudis and Americans against Iran is an indication that this resignation is a new scenario for creating tension in Lebanon and the region.
"But we believe that the resistant people of Lebanon will pass this stage easily.
"The sudden resignation of Mr Hariri and his statement in another country is not only surprising and regrettable but is also an indication of his playing a game designed by those who want ill for the region, and the winner of this game is not Arabic or Muslim countries but the Zionists."
|Hariri's resignation casts doubt on Lebanon's political future [Mohamed Azakir/Reuters]|
Earlier, Hossein Sheikholeslam, another foreign ministry official, said in a tweet: "Hariri's resignation was masterminded by #Trump & Saudi Crown Prince #MBS."
Al Jazeera's Zein Basravi, reporting from Tehran, said: "The alliance between Hezbollah and Iran is no secret to anyone. It began and flourished in the 1980s when Lebanon was occupied by Israeli military forces.
"In more recent times, Hezbollah has been a key strategic partner for Iran in the conflict in Syria and is an important reason why Bashar al-Assad is still in power in Syria.
"Iran's approach to regional conflicts is to speak softly and carry a big stick, and Hezbollah has been an extension of that stick in Lebanon and Syria."
Imad Harb, a political analyst at the Arab Center in Washington, DC, said that for Hariri to make the announcement in Riyadh "basically means he can't have control over his government or his country.
"Hezbollah has been in control of the Lebanese state for quite a while and now it's a supposed victory in Syria on the side of the Syrian regime," Harb told Al Jazeera, referring to Hezbollah's role in fighting alongside forces of Syrian President Bashar al-Assad, who has slowly taken back control over his country following a six-year civil war.
"This has definitely affected Hariri's decision to resign, I have no doubt that maybe he is afraid for his life."
Speaking from Beirut, Kamel Wazne, a Lebanese political analyst, told Al Jazeera: "This is a surprise coup by all measures. Everything was fine within the country.
"There is an election coming up. Everybody is talking about planning for of the economic future of the country, and a call came from Saudi Arabia and everything changed.
"This comes in light of imminent American sanctions against Lebanon, certain threats coming from Israel and escalation by the Saudis.
"I think the prime minister probably caved in to the demands of the Saudis and he declared his resignation from Saudi Arabia. This does not bode very well for the stability of Lebanon."