Tony Parker scored 17 of his 21 points in the second half, and Richard Jefferson added 18 in a defense-dominated meeting of the Western Conference's top two teams.
San Antonio nursed a small lead through most of the second half until Lamar Odom's 3-pointer with 1 minute to play and Pau Gasol's free throws with 22.7 seconds left put the Lakers ahead.
Manu Ginobili missed an open 3-pointer and Tony Parker missed a floater in the lane on the Spurs' final possession, but San Antonio twice got the ball back. Duncan's shot after an inbounds play then bounced high off the back rim, but McDyess slipped inside to tip it home an instant before the buzzer sounded.
"I was in the right place at the right time, and it bounced right to me," said McDyess, who eluded Odom and Kobe Bryant to tip it home. "I haven't had too many (buzzer-beaters). I can probably count them on one hand, but this is a big one."
Gasol scored 19 points after being named to the All-Star team earlier in the day, and Bryant added 16 points on 5-for-18 shooting along with 10 assists and nine rebounds for the Lakers, who trailed by five points with less than three minutes to play.
Los Angeles held the Spurs scoreless in the final 1:53 - until McDyess' improbable tip set off a joyous on-court celebration.
Duncan and McDyess both were stunned when Duncan's fadeaway jumper missed.
"It felt really good on the release, and it was on line," said Duncan, who scored eight points on 3-of-12 shooting. "That's a bunch of emotions in like a second and a half."
The game featured a full third of the West All-Star team for the game at Staples Center on Feb. 20. Bryant, the leading vote-getter, was joined by Gasol, Duncan and Ginobili when the West reserves were named earlier Thursday.
McDyess finished with eight points and eight rebounds - including six offensive boards - while Ginobili had 14 points and eight assists in San Antonio's 12th win in 14 games. Parker was left off the All-Star team, but the French point guard kept the Spurs ahead in their second win this season over Los Angeles.
Odom scored 10 of his 16 points in the fourth quarter for the Lakers, who again came up small in a big game. The two-time defending NBA champions dropped to 1-5 against the four teams above them in the overall standings, including two losses to San Antonio.
Despite the Spurs' steady success and the Lakers' recent struggles, San Antonio coach Gregg Popovich claimed before the game that Los Angeles is the best team in the West. Popovich credits the Spurs' superior record to the fewest injuries for any contending team.
Andrew Bynum had 10 points, 10 rebounds and a career-high six assists in his return to the Lakers' starting lineup after missing one game with a bone bruise in his knee.
But the Lakers had a downbeat ending to their final home date before a seven-game road trip leading up to All-Star weekend at Staples Center, which will be occupied by the Grammy Awards next week.
Popovich was thrilled to hear Duncan had been selected for his 13th straight All-Star team, while Lakers coach Phil Jackson was disappointed Odom wasn't chosen. Jackson thinks Odom should be the commissioner's selection as a replacement for Houston's injured Yao Ming.
"I'd like to see him have a weekend off, personally," Jackson said of Odom. "Pau is great in a forum like that. He won't play that hard, anyway."
NOTES: The Spurs made the second of nine stops on their annual rodeo road trip. ... San Antonio's Gary Neal beat the first-quarter buzzer with an acrobatic, quick-release 15-footer from the baseline after grabbing a loose ball. ... Jackson and Popovich are the only active NBA coaches with multiple championships. ... Fans near courtside included Leonardo DiCaprio, Martin Lawrence, Jonah Hill, director Guillermo del Toro and Andy Garcia.