The win allowed Stanford (14-13, 8-6) to avenge a 77-74 home loss to its San Francisco Bay Area rival in the conference opener in December.
Cal (8-19, 2-12) has lost 12 of 13 since that win.
After the Cardinal extended a 37-36 halftime lead to 63-55 by holding the Bears to just four field goals over a nearly 10-minute stretch of the second half, Cal scratched back within 73-72 on a Justice Sueing jumper with 1:38 to play.
Okpala then connected for the Cardinal to re-establish a three-point lead, and Cal never got another field goal, thanks in part to Sharma blocking Don Coleman's layup attempt with 12 seconds left.
Dorian Pickens gave Stanford its final four-point margin of victory with two free throws with eight seconds remaining, and the Cardinal held on to snap a two-game losing streak.
Daejon Davis had a game-high 22 points to lead Stanford, which had lost its previous four road games. The freshman guard also collected seven rebounds and a game-high five assists.
Reid Travis recorded a double-double for the Cardinal with 13 points and a game-high 10 rebounds, while Pickens finished with 17 points and Michael Humphrey finished with 11.
Stanford won despite committing 16 turnovers.
Coleman led Cal with 17 points, while Marcus Lee grabbed a team-high nine rebounds to complement 15 points for the Bears, who had won their most recent home game 74-70 over Oregon State.
Sueing had 15 points and Juhwan Harris-Dyson 13 for Cal, which shot just 37.1 percent from the field and connected on just three of its 18 3-point attempts.
The Bears entered the game as the Pac-12's worst team in field goal percentage, 3-point percentage and scoring.
Stanford built its one-point halftime lead on the strength of Davis' 3-pointer and two free throws in the final minute of the first period.
The Cardinal led by as many as eight in the half, coming initially when Humphrey and Pickens hit 3-pointers in an 8-0, game-opening run.
--Field Level Media
Stanford | California | |
Daejon Davis 22 | Scoring | Don Coleman 17 |
Daejon Davis 5 | Assists | Don Coleman 4 |
Reid Travis 10 | Rebounds | Marcus Lee 9 |
Daejon Davis 7 | Free Throws Made | Marcus Lee 7 |
Daejon Davis 2 | Steals | Darius McNeill 3 |
Josh Sharma 3 | Blocks | Marcus Lee 3 |
Team | Points | FG% | 3PM-3PA | FTM-FTA | Assists | Rebounds | Blocks | Steals | Turnovers |
Stanford | 77 | 44.8 | 6-15 | 19-25 | 12 | 40 | 6 | 4 | 16 |
California | 73 | 37.1 | 3-18 | 18-22 | 10 | 32 | 6 | 6 | 9 |