When Sony was hacked over The Interview a bunch of emails were leaked including some between Sony and Marvel. Apparently Spiderman was supposed to be in Captain America: Civil War but the deal fell through. When the emails leaked about it fans went nuts a pushed Sony to include Spiderman in the MCU which ended up happening. Sony still has the rights to Spiderman he's just part of the MCU now.
I'd recommend reading the emails. They show just how badly Sony needed Marvel's help to make a good Spiderman. Here's my favorite http://imgur.com/XiqC6fz
Edit: Also Sony was trying to make their own Spiderman universe instead of being part of the Marvel Cinematic Universe.