We investigate the electron states and optical absorption in square- and hexagonal-shaped two-dimensional (2D) HgTe quantum dots and quantum rings in the presence of a perpendicular magnetic field. The electronic structure is modeled by means of the $sp^3d^5s^*$ tight-binding method within the nearest-neighbor approximation. Both bulklike and edge states appear in the energy spectrum. The bulklike states in quantum rings exhibit Aharonov-Bohm oscillations in magnetic field, whereas no such oscillations are found in quantum dots, which is ascribed to the different topology of the two systems. When magnetic field varies, all the edge states in square quantum dots appear as quasibands composed of almost fully flat levels, whereas some edge states in quantum rings are found to oscillate with magnetic field. However, the edge states in hexagonal quantum dots are localized like in rings. The absorption spectra of all the structures consist of numerous absorption lines, which substantially overlap even for small line broadening. The absorption lines in the infrared are found to originate from transitions between edge states. It is shown that the magnetic field can be used to efficiently tune the optical absorption of HgTe 2D quantum dot and quantum ring systems.