The production of beauty and charm quarks in ep interactions has been studied with the ZEUS detector at HERA for exchanged four-momentum squared 5 < Q^2 < 1000 GeV^2 using an integrated luminosity of 354 pb^{-1}. The beauty and charm content in events with at least one jet have been extracted using the invariant mass of charged tracks associated with secondary vertices and the decay-length significance of these vertices. Differential cross sections as a function of Q^2, Bjorken x, jet transverse energy and pseudorapidity were measured and compared with next-to-leading-order QCD calculations. The beauty and charm contributions to the proton structure functions were extracted from the double-differential cross section as a function of x and Q^2. The running beauty-quark mass, m_b at the scale m_b, was determined from a QCD fit at next-to-leading order to HERA data for the first time and found to be 4.07 pm 0.14 (fit} ^{+0.01}_{-0.07} (mod.) ^{+0.05}_{-0.00} (param.) ^{+0.08}_{-0.05} (theo) GeV.