Three Israelis were killed and 16 injured after two Palestinian gunmen opened fire at a crowded bus stop in West Jerusalem, Israeli police say.
Video showed the attackers getting out of a car on a highway and shooting at people with a rifle and a pistol.
Off-duty soldiers and a civilian at the scene killed the gunmen, who police said were from occupied East Jerusalem.
Hamas claimed the pair as its members and said the attack was a response to Israeli "crimes" in Gaza and elsewhere.
It came hours after the Palestinian armed group - which Israel, the UK, US and other Western powers class as a terrorist organisation - agreed with Israel to extend a truce in Gaza for a seventh day and allow another exchange of Israeli hostages for Palestinian prisoners.
US Secretary of State Antony Blinken meanwhile arrived in Tel Aviv as part of efforts to prolong the pause in the devastating war, which was triggered by Hamas's deadly attack on Israel on 7 October.
On Thursday morning Mr Blinken held talks with Israel's President Isaac Herzog, who told him that the Jerusalem shooting was another example of "the endless war that we are fighting against terrorist organisations, especially Hamas, in these very complicated and challenging times".
Mr Blinken said the attack was a reminder "of the threat from terrorism that Israel and Israelis face every single day".
Israeli police said they received reports of gunfire at Givat Shaul junction, on the main highway from Tel Aviv to Jerusalem, at about 07:40 (05:40 GMT).
"A car approached with two armed terrorists - one with an M16 rifle and the other with a handgun," Jerusalem District Commander Doron Turgeman told reporters at the scene.
"At a bus stop they opened fire at people who were waiting there," he said. "The quick response of two soldiers and a civilian, who were close to the stop, neutralised the attackers."
A large amount of ammunition was later found by officers who searched the gunmen's vehicle.
Magen David Adom (MDA) ambulance service paramedic Moshe Hemed rushed to the scene.
"I saw a number of casualties... Some were lying and some were walking. Along with large numbers of MDA teams we assessed and treated them," he said.
"Unfortunately, we had to pronounce a 24-year-old female deceased, and evacuated a further eight casualties in varying conditions to hospitals across the city."
Later, police announced that two other victims of the attack had died and that three more were in a serious condition in hospital.
Israel media identified the 24-year-old woman as Livia Dickman and the two others killed as Rabbi Elimelech Wasserman, 73, and Hana Ifergan, 67.
Police said the attackers were two brothers in their 30s from the Sur Baher area of East Jerusalem.
The Shin Bet security service named them as Murad and Ibrahim Namer and said they were affiliated to Hamas and had previously served prison sentences for involvement in terrorist activities, according to Israeli media.
Visiting the site of the attack, far-right National Security Minister Itamar Ben-Gvir said: "Hamas is speaking with two voices here.
"This event proves again how we must not show weakness, that we must speak to Hamas only through the [rifle] scopes, only through the war."
Mr Ben-Gvir also said the Israeli government would continue to ease regulations for issuing gun licences to citizens.
Several hours later, Hamas said in a statement that two members of its military wing had carried out the attack.
"The operation came as a natural response to unprecedented crimes conducted by the occupation," it added, citing the killing of Palestinians in the Gaza war and during Israeli raids in the occupied West Bank, as well as the treatment of Palestinian prisoners.
Israel launched a major military campaign in Gaza in response to an unprecedented cross-border attack by Hamas gunmen almost eight weeks ago, in which 1,200 people were killed and about 240 taken hostage.
Gaza's Hamas-run government says more than 15,000 people have been killed in the territory since the war began.
A deal mediated by Qatar has led to a pause in the fighting since last Friday.
During that time, Hamas has freed 70 Israeli women and children among the hostages and Israel has released 210 Palestinian women and youths from its jails in return. Ten hostages and 30 prisoners should be freed on Thursday.
Twenty-seven foreign hostages, three of them with Israeli dual citizenship, have also been handed over under separate arrangements.
Before the truce, four Israeli hostages had been released, while Israeli forces in Gaza had rescued one hostage and recovered the bodies of two others.