Does "Recycle Energy" go back into your hand if you discard it as part of a retreat cost? Does it go back into your hand if it's attached to a Pokemon that gets knocked out?
Yes in both cases. But don't be discarding it from your hand as part of an effect or Trainer cost and expect to get it back though. Also, if something sends Recycle Energy to the Lost Zone, you won't get it back from there.
Source: Unified Minds FAQ (2019-07-19), TPCi Rules Team (2019-08-01)

Relevant cards

Note: All applicable cards may not be listed below.
SM11_212
SM11_212
SM11_257
SM11_257