The system RX J0806.3+1527 (HM Cnc) is a pulsating X-ray source with 100 per cent modulation on a period of 321.5 s (5.4 min). This period reflects the orbital motion of a close binary consisting of two interacting white dwarfs. Here we present a series of simultaneous X-ray (0.2-10 keV) and near-ultraviolet (2600 angstrom and 1928 angstrom) observations that were carried out with the Swift satellite. In the near-ultraviolet, the counterpart of RX J0806.3+1527 was detected at flux densities consistent with a blackbody with temperature 27E+3 K. We found that the emission at 2600 angstrom is modulated at the 321.5-s period with the peak ahead of the X-ray one by 0.28 cycles and is coincident within 0.05 cycles with the optical. This phase-shift measurement confirms that the X-ray hot spot (located on the primary white dwarf) is at about 80-100 degrees from the direction that connects the two white dwarfs. Albeit at lower significance, the 321.5-s signature is present also in the 1928-angstrom data; at this wavelength, however, the pulse peak is better aligned with that observed at X-rays. We use the constraints on the source luminosity and the geometry of the emitting regions to discuss the merits and limits of the main models for RX J0806.3+1527.