Is this true?
Yep! See Gravity assist. It's been used dozens of times, and some missions use many assists, sometimes from the same body more than once, and sometime to loose energy rather than gain it!
In these encounters, momentum and energy are still conserved. The spacecraft picks up speed by being pulled towards another much more massive body for a short time, usually hours.
If so, how is this possible? I'm asking because the phenomenon could be used to generate energy from gravity, and obviously we have not been able to do that.
Gravity goes both ways, and so the larger looses the same amount of energy as the spacecraft gains.
In the case of a spacecraft and a planet, the loss is so tiny that it is not measurable as a change in the planet's orbit, but energy is still conserved.