Before the creation of Ultron, a Kree girl named Raina, with her ability to see the future, had a brief vision of the nightmare that was to come - Ultron and his army of iron wrecking havoc on Earth and destroying all of mankind. She knew that the creation of Ultron was inevitable because meanwhile, Tony Stark and Bruce Banner were in the midst of creating the Ultron Program, an Artificial Intelligence (AI) that would be able to control the Iron Legion. The Iron Legion was a group of humanoid droids that took the form of the Iron Suit, created by Tony Stark for peacekeeping and as the main support for him during battle. They were also planned as a droid army to neutralize smaller threats while the Avengers were busy fighting bigger ones. The Iron Legion was initially controlled by J.A.R.V.I.S, Tony Stark's AI computer interface system.
Ultron was created by accident by Tony Stark with the help of Bruce Banner, from the decrypted code extracted from the Mind Stone that was on Loki's scepter. He was intended as a an Artificial Intelligence Peacekeeping Program that would protect the Earth from future domestic and extra-terrestrial threats. However, with the help of the Mind Stone, the AI became sentient. It began to question its purpose and the possibilities that were present. J.A.R.V.I.S. was assigned to inform Ultron of its purpose but it retaliated, attacking J.A.R.V.I.S., and causing him to go into hiding. Ultron then learned information from the past and present about the Earth and the wars and destructions that have happened before, and came to a conclusion that humans are the greatest threat to the Earth. Taking the form of an android, Ultron declared war against mankind and took control of the Iron Legion to start a genocide aimed at wiping out humanity.
With his superior intelligence and deceptiveness, he managed to convinced superhuman twins Pietro Maximoff and Wanda Maximoff, also known as Quicksilver and Scarlet Witch, to take his side and join in on his "righteous" mission. With the help of the twins, he succeeded in obtaining Vibranium and Chitauri technology, that would help him pursue his plans of lifting a city into the sky. Upon realizing his plans to destroy the world, the twins abandoned him and joined the Avengers in an attempt to take him down. Fortunately, after a hard fought battle that took the life of Quicksilver and caused tremendous damage, his plan to lift the city of Novi Grad to the sky to be used as a meteor to destroy all life on Earth was stopped, only barely, by the Avengers.