When Riku stepped into the Door to Darkness, the darkness inside him took over. That gave him command of dark powers, the ability to take on the form Xehanort's Heartless wore, and a Keyblade forged from darkness.
It is less a transformation than a disguise the darkness allowed him — Riku was still in there, which is why he could shed the form later.