Luna always had dark magic inside her. It wasn't HER magic but something she had to keep under control to protect the ponies. This manifested as the dark blotch on her flank. When her jealousy of her sister took over, so did the magic. She became Nightmare Moon. When she got rainbow blasted, the magic went back inside her. Unfortunately, it had grown stronger. the dark blotches grew on her coat. When she gave her magic to Twilight, the blotches didn't disappear because it wasn't her magic to give. But when Starlight switched the princess' cutie marks, the blotches did transfer to Celestia. We know Starlight likes to combine spells. She might have accidentally combined another spell with her switching spell that transferred the magic into Celestia. This later manifested as Daybreaker in the dream realm.