On the basis of total-energy calculations within density functional theory the possibility of magnetic ordering in ultrathin, one or two atom wide nanowires is studied. Specifically, we investigate nanowires composed of fifth and sixth row elements, which are nonmagnetic in the solid phase. At first, the unsupported straight wires are discussed and then, similar to experimental conditions, the wires are placed along step ledges of vicinal surfaces of copper and silver Free-standing wires show only a weak tendency towards magnetic ordering at the equilibrium bond length. In analogy with their 3d homologues Mo, Tc, W, Re are found to order antiferromagnetically, Ru, Rh, and Ir ferromagnetically. Surprisingly, ferromagnetism is also predicted for the early transition metals Zr and Ta and for the simple metals In and Tl. This picture is profoundly modified for supported wires, where the expansion of the bond length enforced through the epitaxial relationship with the substrate favors magnetic ordering but hybridization with the substrate electrons tends to quench magnetism. It turns out that wires on a Cu substrate prefer a ferromagnetic order, whereas on a Ag substrate most elements tend to antiferromagnetism. A second row of atoms added to the wires destroys the magnetism in wires on a Cu substrate, and reduces it in wires on a Ag substrate, except for the late transition metals (Rh, Ir) where an enhancement of magnetic moments is observed. Two possible growth modes of nanowires - a row-by-row growth and island growth - are explored. The results allow us to suggest that Ru, Rh, and Os wires on Ag stepped surfaces are the most promising systems in which magnetism could be verified experimentally.