http://storage.cloversites.com/makinglifecountministriesinc/documents/How%20Did%20the%20Sword%20of%20Goliath%20Get%20to%20Nob_2.pdf
How Did Goliath’s Sword Get to Nob?
After David killed Goliath, David used Goliath’s sword to cut off his
head (1 Samuel 17:51). Then David took Goliath’s weapons back to his
tent (7:53). The Bible doesn’t mention it again until later, when
David is fleeing from King Saul who is trying to kill him.
David goes to the town of Nob where he visits with a priest named
Ahimelech. David told him that he was an urgent mission from the king
and needed bread and a weapon. The priest told him, “The sword of
Goliath the Philistine, whom you killed in the valley of Elah, is
here, wrapped in a cloth behind the ephod. If you want to take it for
yourself, then take it” (1 Samuel 21:9). This sword wasn’t like the
long heavy swords that medieval knights used but was similar to a long
dagger that could be handled easily in battle.
How did Goliath’s sword get into the sanctuary at Nob? The last we
heard of it was when David put it in his tent. Although the Scripture
doesn’t tell us exactly how it got there, David probably dedicated the
sword to God as a symbol of his gratefulness for his conquest of
Goliath. Since there was no temple in Israel at this time, the large
sanctuary at Nob, which was staffed with 85 priests, was the most
likely place to do this.
When David was fleeing from Saul he was in need of a sword. He knew
that priests didn’t have swords, but he also knew that Goliath’s sword
was in the sanctuary at Nob. He had dedicated it to God and now in his
time of need, the priest gave it back to him. No doubt that every time
that David looked at that sword, he was reminded of God’s faithfulness
in defeating Goliath and would continue to be with him in all future
battles.
Edit to answer comment: @Bach Perhaps David didn't presume the sword was still there, so he feigned ignorance. Ahimelech may not have been the priest to whom the sword was first presented for 'dedication'. (The OT doesn't tell us, so we're looking for plausible explanations. I posted this one because it checks a lot of boxes - at least in my mind, thus far.)
What I don't get is why David would have placed so much store on the sword of Goliath to have taken it in the first place. It was not the sword that killed Goliath, but the Hand of God directing the stone in the slingshot. David proceeding to cut off dead Goliath's head only over-shadowed God's awesome proof of Power. David should have left the sword on the ground and praised God - not made a show of himself.
For all we know, the sword could have been stolen from David's tent, and then David heard rumors that it was now in the sanctuary at Nob. Personally, I think the 'dedication' more likely.