A transient molecular dynamics (TMD) method has been developed for simulation of fluid viscosity. In this method a sinusoidal velocity profile is instantaneously overlaid onto equilibrated molecular velocities, and the subsequent decay of that velocity profile is observed. The viscosity is obtained by matching in a least-squares sense the analytical solution of the corresponding momentum transport boundary-value problem to the simulated decay of the initial velocity profile. The method was benchmarked by comparing results obtained from the TMD method for a Lennard-Jones fluid with those previously obtained using equilibrium molecular dynamics (EMD) simulations. Two different constitutive models were used in the macroscopic equations to relate the shear rate to the stress. Results using a Newtonian fluid model agree with EMD results at moderate densities but exhibit an increasingly positive error with increasing density at high densities. With the initial velocity profiles used in this study, simulated transient velocities displayed clear viscoelastic behavior at dimensionless densities above 0.7. However, the use of a linear viscoelastic model reproduces the simulated transient velocity behavior well and removes the high-density bias observed in the results obtained under the assumption of Newtonian behavior. The viscosity values obtained using the viscoelastic model are in excellent agreement with the EMD results over virtually the entire fluid domain. For simplicity, the Newtonian fluid model can be used at lower densities and the viscoelastic model at higher densities; the two models give equivalent results at intermediate densities.