First of all, I agree with the other answers pointing out that it's much safer to just avoid this, and only store hashes of passwords, not the passwords themselves, or anything that can be transformed back to a password.
There are times, however, when you more or less need to allow for recovery. In the case of passwords, you generally want to recover by simply allowing an administrator to change the password when/if needed rather than recovering the existing password.
Another possibility, however, is that that you allow the user to store data on the server that's encrypted with their own password. In this case, simply allowing an administrator to change the password is not sufficient. The new password won't work to decrypt the data, and most users will find it unacceptable for all their encrypted data to become inaccessible when/if they forget/lose a password. For this situation, there's an alternative that's reasonably secure, and still allows recovery when really needed.
Instead of using the user's password to encrypt the data, you create a random key to encrypt the data itself. You then store that key, in a couple of places: once encrypted with the user's password, and in another place encrypted with an administrator password. Then when (not really if) the user loses his/her password and can no longer access the data directly, you can use the administrator password to decrypt the real key and use that to recover the data and/or re-encrypt the key with the user's new password.
If you don't want to trust a single administrator completely, you can manage that as well. For example, you can decide that 5 people will have administrator keys, and you want at least three of them to agree before a key can be recovered. In this case, when you store the encrypted password for administrative purposes, you store it multiple times, once each for each set of three of the five administrators (which doesn't take up much space, since you're only storing multiple keys, at ~256 bits apiece, not multiple copies of the data). Each of those copies is successively encrypted with (the hashes of) each of the passwords for those three administrators.
To decrypt it, you need to identify the three administrators who are entering their passwords, and pick the proper encrypted key for that set of three, then decrypt using each of the three passwords to finally get the original key. You can then use that to recover the data itself, or you can just re-encrypt it with the (hash of) the user's new password so they can still access their data.
When you do this, though, you really need to use a standard encryption algorithm, and (by strong preference) a standard, well-known, thoroughly-studied implementation of it.