The incident happened at about 9:15 a.m. Wednesday in the 800 block of Magnolia Avenue in unincorporated Martinez.
Two deputies in the department's civil unit had gone to the area to evict someone from a home, Lee said.
As one of the deputies was getting out of the car, his gun accidentally went off, causing a superficial injury to his right leg.
The deputy is 39 years old and has been with the sheriff's office for 10 years, Lee said.
The accident is still under investigation, but right now "we have no idea how this happened," Lee said.