We describe a first-principles method for calculating electronic structure, vibrational modes and frequencies, electron-phonon couplings, and inelastic electron transport properties of an atomic-scale device bridging two metallic contacts under nonequilibrium conditions. The method extends the density-functional codes SIESTA and TRANSIESTA that use atomic basis sets. The inelastic conductance characteristics are calculated using the nonequilibrium Green's function formalism, and the electron-phonon interaction is addressed with perturbation theory up to the level of the self-consistent Born approximation. While these calculations often are computationally demanding, we show how they can be approximated by a simple and efficient lowest order expansion. Our method also addresses effects of energy dissipation and local heating of the junction via detailed calculations of the power flow. We demonstrate the developed procedures by considering inelastic transport through atomic gold wires of various lengths, thereby extending the results presented in Frederiksen et al. ͓Phys. Rev. Lett. 93, 256601 ͑2004͔͒. To illustrate that the method applies more generally to molecular devices, we also calculate the inelastic current through different hydrocarbon molecules between gold electrodes. Both for the wires and the molecules our theory is in quantitative agreement with experiments, and characterizes the system-specific mode selectivity and local heating.