Based on the current information I am seeing, army B may be able to take the city and hold it with about 1000 men.
Note that at current time of answer, op has requested that defenders are spaced evenly along the wall, with ~100 men at each gate. This tells me that there is limited space on the walls, and it's more effective to rush a single point than to try and fight all the defending soldiers at once.
With siege engines, which can take down castle walls easily, all you have to do is focus fire. This allows for multiple strategic responses.
Assuming worst case scenario where in 2 days, you'll be crushed by 2500 knights, you need to capture the castle within a day and a half so that you have at least a half day to prepare for the oncoming enemies.
Step one: Focus fire
You have trebuchets and onagers - use them. Other than 2 onagers, focus all projectiles on one point of the wall. By doing so, you open up several scenarios. With the other 2 onagers, put one on each gate and task it to block the gate with boulders so that no one can escape from the castle.
Important thing to remember: the elite troops of Army A are in the catacombs! They're not on the walls! Optimally, we're going to assume that about 500 of them are elite soldiers and as such, only 1000 other troops are left on the walls.
1) Everyone on the defenders side goes to the wall to fix it as it gets destroyed
Your response: Now that everyone has left the opposite side of the castle, your siege tower (or ladders, whichever happens to be stealthier for you) is free to roll up to the wall. Send up 800 of the 1000 soldiers (Pick the most well trained) you have in your army. Your soldiers are now on the wall, and have free reign against archers (which would likely consist of most of the defenders on the wall, since archers are the most useful on walls/in towers (which doesn't seem like the city has any)) and other soldiers. Because most of the enemy will be trying to save the wall your machines are smacking away at, you'll effectively be able to cut them down from behind. I predict that 800 soldiers should be enough for this job simply because a) they won't be expecting it - demoralization is likely, as well as having their backs to you being a massive advantage. b) the numbers advantage is much less in a narrow corridor such as on a wall. As soon as that happens, it's likely that the enemy will drop what they're doing on the wall, and you can stop firing rocks. This is to allow your task force of 200 armored knights to storm inside and take out the 800 ill equipped defenders (assuming ill equipped means badly trained peasants with pitchforks/knives, etc). If the wall hasn't crumbled by this time, you have 2 options: You can either use the battering ram to get through the rest of the wall, or the sappers. This will be possible because everyone is busy fighting your 800 men on the walls.
2) Everyone on the defenders side runs away from the rocks to save themselves
Your response: Split your forces into 750 and 250 men. Stop firing your siege engines. Send 750 men up the side of the wall where the entire enemy force has congregated, and send 250 up the empty side (as quietly as you can). Use the battering rams and the siege tower as cover (and a fat distraction) - stall and distract the enemy force while your 250 men dispatches the enemy from the back. Then, you can take out the militia easily.
3) They don't do anything, the wall falls within an hour, they take massive casualties from the rocks hitting them while they stand on the walls, and you march your men through the wall.
The followup: Marching 1000 men through the wall can easily take out the 800 militia inside the city. At the same time, the rest of the troops have to get off the walls to fight you - archers can't fire into their own city for fear of killing their own people, soldiers on walls are out of range. This means there are now trickles of soldiers coming into the city from the walls, that you can cut down easily with some choke points in the city streets.
Up till this point we've only covered what to do to storm the castle. Now, to hold it:
OP has mentioned that he has sappers, and that there are catacombs below the city. What needs to be done now to hold the city consist of several things:
1) Seal off the catacombs. You don't want those "elite troops" to come up and cut you down. Dispatch some sappers and soldiers to seal off all entrances/exits of the catacombs. They'll starve/drown/suffocate to death down there. In fact, if at all possible during the siege process, you will want to do this step ASAP.
2) Fix the wall (if needed). If you put a hole in the wall, use your sappers to fit it. Better yet, booby trap it! Reinforce it if you can, you can take apart the siege tower/rams and use its parts to do so if necessary.
4) Set up the onagers on the walls, and the trebuchets in the city. Use them to attack enemy siege engines should they bring any, or to wreck general havoc on advancing forces.
3) Send a fast moving scout (or something like that) to your home country asking for reinforcements. This is almost the only way for you to survive indefinitely. When your help arrives, you can commence a pincer attack on the 2500 relief troops. Otherwise, you're in for a long wait until the relief troops leave....
Disclaimer: the above is a very rough strategy based on optimal conditions and responses - without more detail concerning the lays of the land, any obstacles, or troop count/types of the enemy, I cannot devise a more elaborate strategy. One of the main reasons this plan should work is because we know the status of the relief troops. Under normal circumstances, where we don't know if there will be relief troops, then the strategy would certainly involve many more people, such as in O.M's answer. However, since we already know it will take at least 2 days, we don't have to worry about it and can focus solely on taking the castle as fast as possible. While technically you should be able to hold the city for a while with the remaining surviving troops (probably about 600 if you got lucky) This is a bad guideline and you should probably send double or triple the amount of troops to guarantee success. Murphy's law: Anything that can go wrong, will go wrong...
EDIT: After some discussion with friends, a second strategy requiring about 1200 men, but less distraction dependent
Position the trebuchets such that the projectiles roll across the top of the gate area/battlements.
Instead of aiming for the walls, aim for the people on top. This accomplishes several things: 1) takes out troops on top of the wall 2) blocks off reinforcements to sections of wall (if your boulders stay on the wall).
In 1200 century, battering rams had roofs. This means you can send rams and sappers to the front gate to take it down quickly. The roofs also protect (a bit) from falling debris as your trebuchets wreck havoc on the gate area. Onagers can be used as a distraction, and as a terror tactic, lobbing rocks into the castle innards, hopefully taking out some of the militants.
As soon as you break through the gates, you have free reign on the militants, and as stated before, chokepoints can quickly dispatch any incoming trained soldiers as they leave the walls to defend their castle. Remember to send sappers out to seal the catacombs!
The 200 extra men are required b/c you're probably going to lose people moving the rams into place, and a frontal attack means you're likely to lose more soldiers since you don't have the element of surprise.