TUNIS — Tunisia’s Islamist prime minister resigned Thursday, ending the two-year-old rule of his party, which has dominated the political scene since the popular uprising here that initiated the Arab Spring.

The departing prime minister, Ali Larayedh of the Ennahda Party, handed power to a caretaker government that will oversee elections later in the year.

The resignation of the Ennahda government is a setback for the Islamists who wanted to lead the country into elections, but is part of a carefully calibrated political agreement with opposition parties to break months of deadlock. Members of the National Constituent Assembly had been stalling proceedings and demanding the resignation of the government since the assassination of a secular politician, Mohammad Brahmi, last July. It was the second assassination in six months, and opposition groups blamed Mr. Larayedh for what they called his laxity toward Islamist extremist threats.

His resignation is the first time Islamists have agreed to step down in the face of rising public anger after coming to power in the wake of Arab Spring uprisings. Tunisia’s experience, which remains marked by deep divisions between religious and secular constituencies, is in stark contrast to the sectarian-tinged civil war in Syria or the decision by the Egyptian military to outlaw its Islamist opponent, the Muslim Brotherhood. Months of intense negotiations have allowed Tunisia, for now at least, to try to find common ground in the contest for the future character of the state.