JERUSALEM — A Palestinian carried out a stabbing and shooting attack in the occupied West Bank on Sunday, killing an Israeli soldier and a civilian, and ratcheting up tensions ahead of Israeli elections.

The attack came amid a heated campaign for the April 9 ballot in which a former general leading a new, centrist party is challenging the longtime incumbent, Prime Minister Benjamin Netanyahu, over who can provide Israelis with better security.

The situation was already volatile.

On Friday, a fragile calm was restored after two rockets from Gaza were fired at Tel Aviv on the night before, without causing harm.

Israel retaliated swiftly, with strikes at what it said were military sites and compounds belonging to Hamas, the Islamic militant group that controls Gaza. The Israeli military said one of the sites it struck in Gaza was the headquarters for coordinating Hamas activities in the West Bank, where the group has been seeking to foment violence.