Israel and Gaza’s armed groups agreed a ceasefire on Saturday to end a sharp surge in cross-border violence, a Palestinian official said.