Terahertz wireless nanosensor networks (WNSNs) are novel networks interconnecting multiple nano-devices by means of wireless communication. Nanosensors can obtain ultra-high-speed transmission rates using communications in the terahertz band, and medium access control (MAC) protocols play an important role in regulating the access to the terahertz channel and coordinating transmission orders among nanosensors. However, classical MAC protocols are not applicable due to the existing molecular absorption noise in terahertz channel and the very limited energy of nanodevices. In this paper, a distributed energy harvesting-based time division multiple access (DEH-TDMA) protocol is proposed, which aims to overcome the energy limitations of nanosensors and the catastrophic collisions in terahertz WNSNs based on a modulation scheme called time spread on-off keying (TS-OOK). The protocol adopts the piezoelectric energy harvesting system, where a Markov decision process (MDP) model is firstly constructed by considering the remaining energy and the number of packets in the buffer as state information, then the number of transmitted packets and the energy consumption are considered as impacting factors in designing the reward function of MDP model, so each nanosensor can dynamically access the channel according to its own state after solving an optimal strategy. Simulation results show that DEH-TDMA has advantages in extending the network life cycle.