Jamal Fenton added 15 points and the Lobos (21-4, 7-2 Mountain West) improved to 6-0 since a two-game losing streak that started with a setback to SDSU on January 18.
SDSU star Chase Tapley, the conference's leading scorer at 16.2 points per game entering Wednesday, was kept off the board until the final five minutes and had just seven points.
Jamaal Franklin scored 16 points with 10 rebounds to lead the Aztecs (20-5, 6-3) and Tim Shelton had 13. SDSU, which was coming off a loss to UNLV on Saturday, has dropped back-to-back games for the first time in more than two years.
"We played a team that played better than anyone we've played to date," said SDSU coach Steve Fisher. "They beat us. I don't think we lost, they beat us."
New Mexico never trailed after taking an 8-7 lead on Gordon's layup with 15:40 remaining in the first half. The Lobos had a 36-32 lead at the break, but the game was tied, 38-38, on Franklin's layup around 1 1/2 minutes into the second half.
The Lobos then used a 13-0 run led by Williams and Chad Adams to take control of the game. Williams knocked down two three-pointers and Adams scored five of his eight points to help New Mexico to a 51-38 lead.
The advantage reached 15 points but was down to eight when Tapley scored his first points of the game on a layup with 4:42 remaining. Tapley hit a three, then split two defenders for a reverse layup that made it a six-point game with 2:32 left.
"They're tough, especially at home," said Williams. "They're not going to wilt under any runs and we knew (that) even as we got up 15. We run off the court real juiced and everything, but we knew they had a run still in them. And that's exactly what they did."
But New Mexico scored four straight points after that and it was never closer than seven the rest of the way.
SDSU had won its last 11 games following a loss. The Aztecs have lost two in a row for the first time since January of 2010, ending a streak of 79 games that was the fifth-longest in the country behind Kansas, Duke, Temple and Ohio State.
New Mexico leads the all-time series, 39-32...SDSU was trying to sweep the season series with the Lobos for the fourth time in the last seven years.