Impossible to say how they got there, and I am by no means an expert.
Could be that they were living happily in the gravel before, and moving that has caused them to come loose?
Have you checked your water parameters?
You can probably find plenty of solutions to controlling the nematodes now you know what they are, there are also probably medications that are suggested. They are harmless by all accounts, so a more natural method of removal is recommended. Control/reduce feeding, increase cleaning and increase frequency of water changes. See if you can capture many of these things in the siphon.
Aside from the nematodes, it would be worth you considering some decoration and cover for your goldfish, for its happiness and security.