The caputre of the city happened later, as recorded in 2 Samuel 5:6–10. The mention of David taking Goliath’s head to Jerusalem is most likely a parenthetical statement inserted by the author.1 With the narrative being composed and edited sometime after the events that they describe, the author would sometimes place events that occur at a later time2 into the text as if they had already happened, or at a point where he thinks it would be most appropriate to convey a particular message.
54 And David took the head of the Philistine and brought it to Jerusalem, but he put his armor in his tent.