The Cosmic Clones are the shades of dead Marios and Luigis of the past
They've come back to haunt you (the player) for your past failures and reclaim their proper lives. Or, well, they wish they could. They know they can't so instead they try to drag more Marios and Luigis into their undead legion. And if you die by falling into a pit, they follow you, to taunt you as if to say that your failure is inevitable. They know you. They know your movement patterns and that is how they copy your current Mario perfectly. If you complete the level they stalk you through, they vanish, overcome by regret that they were not that particular Mario.
Mario is not aware of any of this until he dies and his own consciousness carries over to his next life. The 1-Up Mushroom prevents you from becoming dead, but doesn't stop death from becoming you.