Basically remember when Voldemort put the Sorting Hat on Neville after Neville confronts him when they all think that Harry is dead? Well, back in COS, Dumbledore کہا that only a true Gryffindor could pull the sword out of the hat, and quite simply Neville was a true Gryffindor which made all the goblin laws null and void and therefore caused the sword to be able to come out of the Sorting Hat. The wonderful Harry Potter parody script author, mollyringwraith, puts it into great terms in her Harry Potter and the Deathly Hallows Parody:
"VOLDEMORT: See, folks? Your little hero's just a limp piece of meat now. NEVILLE: Oi, you! Bite me! VOLDEMORT flicks the Sorting Hat onto NEVILLE's head with a few nasty curses attached. Big mistake. NEVILLE pulls the Sword of Gryffindor out of it. READERS: Hey, how did it end up in there? OTHER READERS: True Gryffindor, time of dire need, goblin rules of ownership null and void, blah blah blah. READERS: All right, all right. Onward."