We propose a nonlinear voter model to study the emergence of global consensus in opinion dynamics. In our model, agent i agrees with one of binary opinions with the probability that is a power function of the number of agents holding this opinion among agent i and its nearest neighbors, where an adjustable parameter alpha controls the effect of herd behavior on consensus. We find that there exists an optimal value of alpha leading to the fastest consensus for lattices, random graphs, small-world networks and scale-free networks. Qualitative insights are obtained by examining the spatiotemporal evolution of the opinion clusters. (C) 2011 Elsevier B.V. All rights reserved.