Miami won a playoff-style game Wednesday at home against Oklahoma City, but never led and faded early Friday against a team fighting to ensure a post-season berth.
Gay scored 17 points and both Mike Conley and Marreese Speights had 15 for Memphis, which won for the fourth time in five games.
The 17 straight home wins made up the longest such streak in the NBA this season and came within one of equaling Miami's club record. The Heat won 18 in a row during the 2004-05 campaign. Miami had not lost at home since Jan. 22.
LeBron James led Miami with 21 points and Dwyane Wade had 20.
Memphis was in front by as many as 19 in the opening half and the Heat came no closer than 11 during the final two periods.
Miami's home record for the season slipped to 23-3.
Ray Liotta sues skin care company over use of likeness
Boston schools pull out free condoms over wrapping complaints