Either cold email them, or try to get an intro through your network (= how you would contact any business for anything). Then you pitch them your skillset and your daily/hourly rate, and they'll decide to place you on their freelance/consulting roster or not.
There is a small split for how you would freelancing vs. consulting via agencies in my experience.
For consulting, you will usually only be brought on if you have a special skill set that the agency themselves can't fill out, and often only if their client specially requests those services. So consulting work via agencies is much rarer in my experience.
For freelancing, as long as the agency is doing well there is almost always something to do, and ~20% of their tech teams consist of freelancers, so it's not really "surplus work" but more their part of their normal mode of operation.
Of course, all of this are just anecdotal data, and may vary widely from region to region.
Yes. Agencies pitch all the time to find their own clients, so they are not averse to getting pitched themselves.
A cold email better be great to work. A better bet is to go to meetups and local conferences for like Wordpress or SEO or digital marketing. You’re likely to meet some agency folks or people who contract with agencies. Chat them up, offer to buy them lunch, stay in contact.
Or, just look up agencies who are hiring for developers. Contact them and offer your contracting services.
A good portfolio is key. Strong client references are even better but it takes time to build those.
You can also do thought leadership type stuff like blog about topics, or participate in regional online communities.
Get out to networking events, local business chambers, local community organiations, etc.
For example: One of our oldest business associates, who has passed over a lot of work over the years, we met volunteering for the local seafood festival.