Wireless sensor network (WSN) is vulnerable to a wide range of attacks due to its natural environment and inherent unreliable transmission. To protect its security, intrusion detection systems (IDSs) have been widely deployed in such a wireless environment. In addition, trust-based mechanism is a promising method in detecting insider attacks (e.g., malicious nodes) in a WSN. In this paper, we thus attempt to develop a trust-based intrusion detection mechanism by means of Bayesian model and evaluate it in the aspect of detecting malicious nodes in a WSN. This Bayesian model enables a hierarchical wireless sensor network to establish a map of trust values among different sensor nodes. The hierarchical structure can reduce network traffic caused by node-to-node communications. To evaluate the performance of the trust-based mechanism, we analyze the impact of a fixed and a dynamic trust threshold on identifying malicious nodes respectively and further conduct an evaluation in a wireless sensor environment. The experimental results indicate that the Bayesian model is encouraging in detecting malicious sensor nodes, and that the trust threshold in a wireless sensor network is more dynamic than that in a wired network.