© PA Photos
Yann Thomas scored the only try of the first half for Gloucester, but mistakes gave Ospreys five penalties, resulting in a two-point lead for the hosts at half-time.
Ospreys looked to be in a strong position holding a 26-13 lead with 10 minutes to go.
A Dan Murphy try brought Gloucester back into the match, and a penalty try three minutes from full-time put the visitors ahead.
Morgan Allen got the ball in the last seconds of the game to claim the win for Ospreys.