The notes of the scale evolved gradually. Originally only the 7 notes of the diatonic (major) scale were used, sometimes fewer than seven. But if you take one of the notes in a diatonic scale other than the first and try to start a new scale starting with this note, you will find that you need new notes, the existing 7 don't allow you to create a new scale that "sounds the same" (has the same ratios between the frequencies of each note). The new notes that need to be added to allow these new scales became what are today the accidentals.
This link has some information about this:
http://en.wikipedia.org/wiki/Accidental_%28music%29#History_...