MySQL can't JOIN properly because of the OR in your query.
To overcome this you can use UNION instead
SELECT photos.url, photos.caption FROM photos JOIN friends ON friend1 = '$userid' && friend2 = photos.userid
SELECT photos.url, photos.caption FROM photos JOIN friends ON friend1 = photos.userid && friend2 = '$userid'
Of course you need indices on friends.friend1, friends.friend2 and photos.userid
I'm guessing (or rather, hoping) you defined friends.friend1, friends.friend2 as the primary key of friends. This is okay.
PS. Union by default removes duplicates. So if you (exactly) have (1,2) and (2,1) in the friends table you only get one result, not two, which is what you want.
So there is no need to use DISTINCT in some way.