We investigate the collapse and internal structure of dark matter halos. We consider halo formation from initially scale-free perturbations, for which gravitational collapse is self-similar. Fillmore and Goldreich (1984) and Bertschinger (1985) solved the one dimensional (i.e. spherically symmetric) case. We generalize their results by formulating the three dimensional self-similar equations. We solve the equations numerically and analyze the similarity solutions in detail, focusing on the internal density profiles of the collapsed halos. By decomposing the total density into subprofiles of particles that collapse coevally, we identify two effects as the main determinants of the internal density structure of halos: adiabatic contraction and the shape of a subprofile shortly after collapse; the latter largely reflects the triaxiality of the subprofile. We develop a simple model that describes the results of our 3D simulations. In a companion paper, we apply this model to more realistic cosmological fluctuations, and thereby explain the origin of the nearly universal (NFW-like) density profiles found in N-body simulations.