Time synchronization in wireless sensor networks (WSN) is essential to schedule communication and distributed measurement tasks. In this paper a consensus-based synchronization algorithm is used to compensate dynamically both time offsets and clock frequency skews, thus driving all WSN clocks towards a common time scale. The proposed approach does not require either the election of a master node as a time reference for the whole network, or a specific synchronization protocol. Some simulation results show that the algorithm converges within a reasonable time, regardless of timestamping jitter, random communication latencies and incomplete node visibility.