We compute the electric dipole moment of proton and neutron from lattice QCD simulations with N_f=2 flavors of dynamical quarks at imaginary vacuum angle theta. The calculation proceeds via the CP odd form factor F_3. A novel feature of our calculation is that we use partially twisted boundary conditions to extract F_3 at zero momentum transfer. As a byproduct, we test the QCD vacuum at nonvanishing theta.