We present a formulation and implementation of second-order quasidegenerate N-electron valence perturbation theory (QDNEVPT2) that provides a balanced and accurate description of spin-orbit coupling and dynamic correlation effects in multiconfigurational electronic states. In our approach, the energies and wave functions of electronic states are computed by treating electron repulsion and spin-orbit coupling operators as equal perturbations to the nonrelativistic complete active-space wave functions, and their contributions are incorporated fully up to the second order. The spin-orbit effects are described using the Breit-Pauli (BP) or exact two-component Douglas-Kroll-Hess (DKH) Hamiltonians within spin-orbit mean-field approximation. The resulting second-order methods (BP2- and DKH2-QDNEVPT2) are capable of treating spin-orbit coupling effects in nearly degenerate electronic states by diagonalizing an effective Hamiltonian expanded in a compact non-relativistic basis. For a variety of atoms and small molecules across the entire periodic table, we demonstrate that DKH2-QDNEVPT2 is competitive in accuracy with variational two-component relativistic theories. BP2-QDNEVPT2 shows high accuracy for the second- and third-period elements, but its performance deteriorates for heavier atoms and molecules. We also consider the first-order spin-orbit QDNEVPT2 approximations (BP1- and DKH1-QDNEVPT2), among which DKH1-QDNEVPT2 is reliable but less accurate than DKH2-QDNEVPT2. Both DKH1- and DKH2-QDNEVPT2 hold promise as efficient and accurate electronic structure methods for treating electron correlation and spin-orbit coupling in a variety of applications.