Formation control and obstacle avoidance for multi-agent systems have attracted more and more attention. In this paper, the problems of formation control and obstacle avoidance are investigated by means of a consensus algorithm. A novel distributed control model is proposed for the multi-agent system to form the anticipated formation as well as achieve obstacle avoidance. Based on the consensus algorithm, a distributed control function consisting of three terms(formation control term, velocity matching term, and obstacle avoidance term) is presented. By establishing a novel formation control matrix, a formation control term is constructed such that the agents can converge to consensus and reach the anticipated formation. A new obstacle avoidance function is developed by using the modified potential field approach to make sure that obstacle avoidance can be achieved whether the obstacle is in a dynamic state or a stationary state. A velocity matching term is also put forward to guarantee that the velocities of all agents converge to the same value. Furthermore, stability of the control model is proven. Simulation results are provided to demonstrate the effectiveness of the proposed control.