Synchronization to an external time source such as Coordinated Universal Time (UTC), i.e., external synchronization, while preserving tight synchronization among neighboring sensor nodes may be crucial for applications such as determining the speed of a moving object in wireless sensor networks. However, existing time synchronization protocols in the literature, which can be used for external synchronization, poorly synchronize neighboring nodes. On the other hand, the only protocol that aims at optimizing the synchronization error between neighboring nodes is lack of a mechanism which synchronizes sensor nodes to a reference node, and hence, it cannot provide external synchronization. Therefore, there is a lack in the literature of a time synchronization protocol, which can be used by applications demanding both external synchronization and tight synchronization among neighboring nodes. In this paper, we answer the question of whether it is possible for sensor nodes to synchronize to a reference node while they optimize the clock skew between their neighboring nodes at the same time. Within this context, we present a novel time synchronization protocol, namely External Gradient Time Synchronization Protocol (EGSync). In EGSync, each sensor node synchronizes to a reference node by using time information flooded by this node, as well as synchronizes to its neighboring nodes by employing the agreement algorithm. We implemented EGSync on the MICAz platform using TinyOS and evaluated it on a testbed setup including 20 sensor nodes. We present the experimental results on our testbed and the simulation results for networks with larger diameters and densities.