Laws are clear. If the attacking player is in an offside position, and it rebounds off a defender, then there is an offside offence for gaining an advantage.
If the ref judges that the defender deliberately played the ball, there is no offside offence.