As (confusingly) explained in "Let's Kill Hitler", after Melody Pond regenerated in New York, she somehow tracked down Amy & Rory to Leadworth. We've never been told how she managed this, only that it took her "years". Once she'd found them, she moved to Leadworth herself, appearing to be a child of about the same age as them & still using the name Melody (though not the surname Pond), which was usually shortened to "Mels". The three of them -- Rory, Amy & Melody -- grew up together. Amy & Melody became best friends & Melody helped to get Amy & Rory together as a couple.
When Amy gave birth to her daughter on Demons Run (between the episodes "The Almost People" & "A Good Man Goes to War"), she named the newborn baby Melody, after her childhood friend. At that point, Amy didn't know that her childhood friend Mels actually was her daughter but the upshot was that Amy had named her daughter after her daughter.
This is the type of temporal paradox sometimes known as the "bootstrap paradox" or (in fancier language) the "ontological paradox", where the result of something becomes the cause of that something. It happens quite a lot in the DW universe & (unlike most other types of temporal paradox) it can sustain itself -- that is, it actually works.