Recently, mediation-aided composition has been widely adopted when dealing with incompatibilities of services. However, existing approaches suffer from state space explosion in compatibility verification and cannot automatically generate the BPEL code. This paper presents a Petri net approach to mediation-aided composition of Web services. First, services are modeled as open WorkFlow Nets (oWFNs) and are composed using mediation transitions (MTs). Second, the modular reachability graph (MRG) of composition is automatically constructed and the compatibility is analyzed, so that the problem of state space explosion is significantly alleviated. Furthermore, an Event-Condition-Action (ECA) rule-based technique is developed to automatically generate the BPEL code of the composition, which can significantly save the time and labor of designers. Finally, the prototype system has been developed.