How was magic discovered?

How do you think magic was discovered? what was the first person who casted magic was thinking? was it a mistake, or was it done intentionally?