You don't say what rules you play under, but the NBA rules are fairly standard here.
From Rule 8:
The ball is caused to go out-of-bounds by the last player to touch it (including by touching the player’s hair or uniform) before it goes out, provided it is out-of-bounds because of touching something other than a player. If the ball is out-of-bounds because of touching a player who is on or outside a boundary, such player caused it to go out. If a player has his hand in contact with the ball and an opponent hits that part of the hand causing the ball to go out-of-bounds, the team whose player had his hand on the ball will retain possession.
There is no special exception for the rim, and no other part of Rule 8 mentions the rim. So, yes, if a defender blocks a shot into the rim, and then it goes out of bounds, the offense would regain possession. In fact, if the shooter's hand is hit by the defender, causing the ball to go out of bounds, but the defender doesn't actually touch the ball, the offense still retains possession.