Israel and Hamas, the Islamist group that rules the Gaza Strip, agreed on a truce on Thursday to end a flare-up in cross-border fighting, two Palestinian officials said.